diff --git a/C/heapsort.c b/C/heapsort.c new file mode 100644 index 00000000..655e6d47 --- /dev/null +++ b/C/heapsort.c @@ -0,0 +1,111 @@ +#include + + + + + + + + + + + + + +void heapify(int arr[],int n, int i) +{ + int q; + int largest=i; + int left=2*i+1; + int right= 2*i+2; + + + + if(leftarr[largest]) + { + largest=left; + } + + + if(rightarr[largest]) + { + largest=right; + } + + if(largest!=i) + { + q=arr[i]; + arr[i]=arr[largest]; + arr[largest]=q; + heapify(arr,n, largest); + } + + +} + + + +void heapsort(int arr[],int n) +{ + int i,q; + + + + for(i=(n/2)-1;i>=0;i--) + { + + heapify(arr,n,i); + } + + for(i=n-1;i>0;i--) + { + + q=arr[0]; + arr[0]=arr[i]; + arr[i]=q; + heapify(arr,i,0); + } + + +} + + + + +void print(int arr[],int n) +{ + int i; + for(i=0;i