-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsrtheap.c
More file actions
54 lines (43 loc) · 1.37 KB
/
srtheap.c
File metadata and controls
54 lines (43 loc) · 1.37 KB
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
/**
* srtheap.c file
*
* to compile with heap sort:
* gcc -std=c99 -DRAND -DTYPE=double -DHEAP *.c
*
*/
#include <stdlib.h>
#include <string.h>
#include "srt.h"
void heapify(char *array, size_t nelem, size_t size, size_t parent, int (*compar)(const void *, const void *));
void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
char *array;
size_t i;
/* build heap */
array = malloc((size + 1) * nelem);
memcpy(array + size, base, nelem * size);
for (i = nelem / 2; i >= 1; i--) {
heapify(array, nelem, size, i, compar);
}
/* sort */
for (i = nelem; i > 1; i--) {
/* move the largest one to the last position */
swap(array + size, array + i * size, size);
heapify(array, i - 1, size, 1, compar);
}
memcpy(base, array + size, nelem * size);
free(array);
}
void heapify(char *array, size_t nelem, size_t size, size_t parent, int (*compar)(const void *, const void *)) {
size_t child;
while ((child = 2 * parent) <= nelem) {
if (child + 1 <= nelem && compar(array + child * size, array + (child + 1) * size) < 0) {
child = child + 1; /* right child is larger */
}
if (compar(array + parent * size, array + child * size) < 0) {
swap(array + parent * size, array + child * size, size); /* child is larger, swap */
parent = child;
} else {
break;
}
}
}