0% found this document useful (0 votes)
2 views4 pages

Heap Sort Assignment

The document contains a C program for implementing Heap Sort, authored by Anik Chakraborty. It includes functions for swapping elements, adjusting the heap, making the heap, and printing the array, as well as the main function to execute the sorting process. The program prompts the user to input an array of integers, displays the heap, and shows the sorted array after applying Heap Sort.

Uploaded by

canik104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Heap Sort Assignment

The document contains a C program for implementing Heap Sort, authored by Anik Chakraborty. It includes functions for swapping elements, adjusting the heap, making the heap, and printing the array, as well as the main function to execute the sorting process. The program prompts the user to input an array of integers, displays the heap, and shows the sorted array after applying Heap Sort.

Uploaded by

canik104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Heap Sort

Name : Anik Chakraborty


Cse_2C_193
A) #include<stdio.h>
#define SIZE 100
void makeheap();
void swap();
void adjust();
void heapSort();
void printArray();

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}
void adjust(int *a,int n,int p)
{
int i,j,t;
i=p;
j=2*i+1;
while(j<n)
{
if(j<n-1&&a[j]<a[j+1])
j++;
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
i=j;
j=2*j+1;
}
}

void heapSort(int a[], int n)


{
int i;

for ( i = n - 1; i >= 0; i--) {


swap(&a[0], &a[i]);
printf("After Every pass heap ");
printArray(a, n);
adjust(a,i,0);
}

}
void makeheap(int a[],int n)
{
int i;
for(i=n/2-1;i>=0;i--)
adjust(a,n,i);

}
void printArray(int a[], int n) {
int i;
for ( i = 0; i < n; ++i)
printf("%d ", a[i]);
printf("\n");
}

int main()
{
int a[SIZE],n,i;
printf("\n Enter the number of element:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The entered elements are:\n");
printArray(a, n);
makeheap(a,n);
printf("\nElements after making heap are:\n");
printArray(a, n);
heapSort(a,n);
printf("\nElements after heap Sort are:\n");
printArray(a, n);
return 0;
}

You might also like