1.MAX_HEAPIFY: /** * adjust the max heap * @param arrays the arrays to sort * @i the index to start check * return nil */ void MAX_HEAPIFY(SortValue arrays[], int i, int size) { //left index int l = LEFT(i); //right index size int r = RIGHT(i); //the index of the max int largest = 0; //get the index of max among the three nodes:i,l,r if (l <= size && int_compare(arrays[l], arrays[i]) > 0) { largest = l; } else largest = i; if (r <= size && int_compare(arrays[r], arrays[largest]) > 0) { largest = r; } if (largest != i) { swap(arrays[i], arrays[largest]); MAX_HEAPIFY(arrays, largest, size); } } 2.BUILD_MAX_HEAP: /** * build the max heap using the array * @param arrays the arrays to sort * @n the length of the array * return nil */ void BUILD_MAX_HEAP(SortValue arrays[], int n) { int i = 0; for (i = ((n - 1) >> 1); i >= 0; i--) { MAX_HEAPIFY(arrays, i, n); } } 3.HEAP_SORT: /** * HEAP_SORT call BUILD_MAX_HEAP(from the 0 index ) * @param arrays the arrays to sort * @n the length of the array * return nil */ void HEAP_SORT(SortValue arrays[], int n) { //build the heap using the array BUILD_MAX_HEAP(arrays, n); int i = 0; for (i = n; i >= 0; i--) { //put the max to the last swap(arrays[0], arrays[i]); //decrease the heap-size MAX_HEAPIFY(arrays, 0, i - 1); } } 4.METHOD: int main(void) { int p1 = 16; int p2 = 4; int p3 = 10; int p4 = 14; int p5 = 7; int p6 = 9; int p7 = 3; int p8 = 2; int p9 = 8; int p10 = 1; void* arrays[] = { &p1, &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10 }; //printf("result is:%d", int_equal(&p1, &p2)); //InsertSort(arrays, sizeof(arrays) / sizeof(void*)); //SelectionSort(arrays, sizeof(arrays) / sizeof(void*)); //MERGE_SORT(arrays, 0, sizeof(arrays) / sizeof(void*) - 1); // /MERGE(arrays, 0, 2, 5); //MAX_HEAPIFY(arrays, 1, sizeof(arrays) / sizeof(void*)); //BUILD_MAX_HEAP(arrays,sizeof(arrays) / sizeof(void*)); HEAP_SORT(arrays,sizeof(arrays) / sizeof(void*)-1); int i = 0; for (i = 0; i < sizeof(arrays) / sizeof(void*); i++) { printf("%d ", (int) *((int*) arrays[i])); } return EXIT_SUCCESS; }