-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathHeap_Sort_Big.cpp
More file actions
98 lines (85 loc) · 1.88 KB
/
Copy pathHeap_Sort_Big.cpp
File metadata and controls
98 lines (85 loc) · 1.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
struct heap_t{
int size;
int capacity;
int *elems;
};
heap_t* createHeap(const int capacity)
{
heap_t *h = (heap_t*)malloc(sizeof(heap_t));
h->size = 0;
h->capacity = capacity;
h->elems = (int*) malloc(sizeof(int) * capacity);
return h;
}
void heapDestroy(heap_t *h)
{
free(h->elems);
free(h);
}
void heap_sift_down(const heap_t *h, const int start)
{
int i = start, j;
const int tmp = h->elems[start];
for(j = 2*i+1; j < h->size; j = 2*i+1){
//get the bigger child
//if j = h->size - 1, then no right child of node i, so skip the if
if(j < h->size - 1
&& h->elems[j+1] > h->elems[j]) j = j + 1;
if(tmp >= h->elems[j]){
break;
}else{
//elems[j] is maximum, move upward
h->elems[i] = h->elems[j];
i = j;
}
}
h->elems[i] = tmp;
}
void heap_sift_up(const heap_t *h, const int start)
{
int j = start;
int i = (j - 1) / 2;
const int tmp = h->elems[start];
for(; i >= 0; i = (j - 1) / 2){
if(h->elems[i] > tmp){
break;
}else{
h->elems[j] = h->elems[i];
j = i;
}
}
h->elems[j] = tmp;
}
void heap_sort(int a[], const int n)
{
heap_t *h;
h = createHeap(n);
int *old_memory = h->elems;
h->elems = a;
h->size = n;
//construct the heap
int i = (h->size - 2) / 2;
while(i >= 0){
heap_sift_down(h, i);
--i;
}
//pop to sort
for(i = h->size - 1; i > 0; --i){
swap(h->elems[i], h->elems[0]);
h->size--;
heap_sift_down(h, 0);
}
h->elems = old_memory;
heapDestroy(h);
}
int main()
{
int a[] = {2, 0, 3, 4, 5, 6, 1, 9, 7, 8};
heap_sort(a, 10);
for(int i = 0; i < 10; ++i)
cout<<a[i] <<" ";
cout<<endl;
return 0;
}