-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.c
73 lines (69 loc) · 2.06 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "heapsort.h"
void heap_sort(data array[], int len) {
int i, heaplen, start;
data tmp; // temporary variable used to swap elements
for (start = 0; 2*start+2 < len; start = 2*start+2) ;
for (i = start; i >= 0; i--)
siftDown(array, len, i);
for (heaplen = len; heaplen > 1; heaplen--) {
tmp = array[0];
array[0] = array[heaplen-1];
array[heaplen-1] = tmp;
siftDown(array, heaplen-1, 0);
}
}
void heap_sort_debug(data array[], int len, STAT *stat) {
int i, heaplen, start;
data tmp; // temporary variable used to swap elements
stat->space = 1;
stat->allo++;
for (start = 0; 2*start+2 < len; start = 2*start+2) ;
for (i = start; i >= 0; i--)
siftDown_debug(array, len, i, stat);
for (heaplen = len; heaplen > 1; heaplen--) {
tmp = array[0];
array[0] = array[heaplen-1];
array[heaplen-1] = tmp;
siftDown_debug(array, heaplen-1, 0, stat);
stat->swap++;
}
}
void siftDown(data array[], int len, int pos) { // warning: working with a MAX-HEAP
data tmp; // temporary variable used to swap elements
int newpos;
while ((newpos = 2*pos+1) < len) { // while pos is not a leaf
if (newpos+1 < len && array[newpos] < array[newpos+1]) // if right child exists and is less than left child
newpos++; // point to right child
if (array[pos] < array[newpos]) { // if father < left child
tmp = array[pos];
array[pos] = array[newpos];
array[newpos] = tmp;
pos = newpos;
}
else
pos = len;
}
}
void siftDown_debug(data array[], int len, int pos, STAT *stat) { // warning: working with a MAX-HEAP
data tmp; // temporary variable used to swap elements
stat->space = 2;
stat->allo++;
int newpos;
while ((newpos = 2*pos+1) < len) { // while pos is not a leaf
if (newpos+1 < len)
if (array[newpos] < array[newpos+1]) { // if right child exists and is less than left child
newpos++; // point to right child
stat->comp++;
}
if (array[pos] < array[newpos]) { // if father < left child
tmp = array[pos];
array[pos] = array[newpos];
array[newpos] = tmp;
pos = newpos;
stat->swap++;
}
else
pos = len;
stat->comp++;
}
}