#include #include #include /* max heap implementation */ /* compute child 0 or 1 */ #define Child(x, dir) (2*(x)+1+(dir)) /* float value at position pos down */ static void floatDown(int n, int *a, int pos) { int x; /* save original value once */ x = a[pos]; for(;;) { if(Child(pos, 1) < n && a[Child(pos, 1)] > a[Child(pos, 0)]) { /* maybe swap with Child(pos, 1) */ if(a[Child(pos, 1)] > x) { a[pos] = a[Child(pos, 1)]; pos = Child(pos, 1); } else { /* x is bigger than both kids */ break; } } else if(Child(pos, 0) < n && a[Child(pos, 0)] > x) { /* swap with Child(pos, 0) */ a[pos] = a[Child(pos, 0)]; pos = Child(pos, 0); } else { /* done */ break; } } a[pos] = x; } /* construct a heap bottom-up */ static void heapify(int n, int *a) { int i; for(i = n-1; i >= 0; i--) { floatDown(n, a, i); } } /* sort an array */ void heapSort(int n, int *a) { int i; int tmp; heapify(n, a); for(i = n-1; i > 0; i--) { /* swap max to a[i] */ tmp = a[0]; a[0] = a[i]; a[i] = tmp; /* float new a[0] down */ floatDown(i, a, 0); } } #define N (100) #define MULTIPLIER (17) int main(int argc, char **argv) { int a[N]; int i; if(argc != 1) { fprintf(stderr, "Usage: %s\n", argv[0]); return 1; } for(i = 0; i < N; i++) { a[i] = (i*MULTIPLIER) % N; } for(i = 0; i < N; i++) { printf("%d ", a[i]); } putchar('\n'); heapSort(N, a); for(i = 0; i < N; i++) { printf("%d ", a[i]); } putchar('\n'); return 0; }