-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.c
57 lines (43 loc) · 1.16 KB
/
heapsort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "heapsort.h"
void swap(Record** r1, Record** r2);
// Generic heapsort of Record array.
void heap_sort(Record** array, int end,int (*comparator)(Record*, Record*)){
heapify(array, end, comparator);
for(int i = end;i>0;i--){
swap(&array[0], &array[i]);
end--;
heap_fixup(array, end, 0 ,comparator);
}
}
void heapify(Record** array, int end,int (*comparator)(Record*, Record*)){
for(int i = end/2; i>=0;i--){
heap_fixup(array, end, i, comparator);
}
}
void heap_fixup(Record** array,int size, int i, int (*comparator)(Record*, Record*)){
int left = left_child(i);
int right = right_child(i);
int max;
if(left <= size &&(*comparator)(array[left], array[i]) > 0){
max = left;
} else max = i;
if(right <= size &&(*comparator)(array[right], array[max]) > 0){
max = right;
}
if(max != i){
swap(&array[i], &array[max]);
heap_fixup(array, size, max, comparator);
}
}
int parent(int i){
return (i-1)/2;
}
int left_child(int i){
return 2*i + 1;
}
int right_child(int i){
return 2*i + 2;
}