-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_heapsort_dumb.c
More file actions
85 lines (67 loc) · 1.67 KB
/
Copy path11_heapsort_dumb.c
File metadata and controls
85 lines (67 loc) · 1.67 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// it is possible to implement this with input[] same as a[] i.e inplace - skip for now
// for heap 1 based index is better
#include <stdio.h>
#include <limits.h>
#define maxN 999
static int N; // static are initialized to 0
static int a[maxN];
void upheap(int k) {
a[0] = INT_MAX;
int v = a[k];
while(a[k/2]<v) {
a[k] = a[k/2];
k = k/2;
}
a[k] = v;
}
void insert(int v) {
a[++N] = v;
//printf("%d ", N);
upheap(N);
}
void downheap(int k) {
int v = a[k];
while (k <= N/2) {
int j = 2*k;
if (j+1<=N && a[j]<a[j+1]) ++j;
// now j points to larger of the 2 children
if (a[j] <= v) break;
a[k] = a[j];
k = j;
}
a[k] = v;
}
int remove0() {
//remove the largest
int v = a[1];
a[1] = a[N--];
downheap(1);
return v;
}
void construct(int input[], int input_size) {
// construct a heap using input[input_size]
N = 0;
a[N] = INT_MAX;
for(int i=0; i!=input_size; ++i) {
insert(input[i]);
}
}
void heapsort(int input[], int input_size) {
construct(input, input_size);
for(int i=input_size-1; i>=0; --i) {
input[i] = remove0();
}
}
int main() {
int input[] = {'X', 'T', 'O', 'G', 'S', 'M', 'N', 'A', 'E', 'R', 'A', 'I'};
//int input[] = {'A', 'A', 'E', 'G', 'I', 'M', 'N', 'O', 'R', 'S', 'T', 'X'};
int input_size = sizeof(input)/sizeof(input[0]);
for(int i=0; i!=input_size; ++i) printf("%c ", input[i]);
printf("\n");
heapsort(input, input_size);
//expected ans: A A E G I M N O R S T X
//output:
for(int i=0; i!=input_size; ++i) printf("%c ", input[i]);
printf("\n");
return 0;
}