Last active
November 29, 2023 06:16
-
-
Save zeerorg/e0b46e9865072e216fce61c77505876b to your computer and use it in GitHub Desktop.
Heap sort in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
void swapArr(int A[100], int n, int m) | |
{ | |
int temp = A[n]; | |
A[n] = A[m]; | |
A[m] = temp; | |
} | |
void heapify(int A[100], int n, int i) | |
{ | |
int left = 2*i + 1, right = 2*i + 2; | |
int large = i; | |
if(left < n && A[left] > A[large]) large = left; | |
if(right < n && A[right] > A[large]) large = right; | |
if(large != i) | |
{ | |
swapArr(A, i, large); | |
heapify(A, n, large); | |
} | |
} | |
void makeHeap(int A[100], int n) | |
{ | |
int i; | |
for( i = (n-1)/2; i>=0; i++ ) | |
{ | |
heapify(A, n, i); | |
} | |
} | |
void deleteMax(int A[100], int n) | |
{ | |
int x = A[0]; | |
swapArr(A, 0, n-1); | |
n = n-1; | |
heapify(A, n, 0); | |
} | |
void HeapSort(int A[100], int n) | |
{ | |
makeHeap(A, n); | |
int i = 0; | |
for(i = 0; i < n; i++) | |
deleteMax(A, n); | |
} | |
int main() | |
{ | |
int A[] = {5, 11, 9, 10, 3, 6}; | |
int n = 6; | |
HeapSort(A, n); | |
int i = 0; | |
for(i=0; i<n; i++) | |
printf("%d ", A[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Segmentation fault