-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheapsort.cpp
57 lines (45 loc) · 1.1 KB
/
heapsort.cpp
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
/**
* heapsort.cpp
*
* Daniel Takayama
* October 24, 2015
*/
#include "heapsort.h"
using namespace std;
void heapsort(vector<int> &vec) {
//make vec into min heap
vector<int> sorted;
heapify(vec);
//repeatedly pop max element off heap and push into sorted list
while (vec.size() != 1) {
sorted.push_back(popMax(vec));
}
//swap contents of vec with sorted
vec.swap(sorted);
}
void siftDown(vector<int> &vec, size_t k) {
int size = vec.size() - 1;
while (2 * k <= size) {
size_t j = 2 * size_t(k);
if (j < size && vec[j] > vec[j + 1]) j++;
if (vec[j] > vec[k]) break;
swap(vec[k], vec[j]);
k = j;
}
}
void heapify(vector<int> &vec) {
//push back filler element and swap with 0th position
vec.push_back(0);
swap(vec[0], vec[vec.size() - 1]);
//fix down on each node
for (size_t i = (vec.size() - 1)/2; i > 0; --i) {
siftDown(vec, i);
}
}
int popMax(vector<int> &vec) {
int max = vec[1];
swap(vec[1], vec[vec.size() - 1]);
vec.pop_back();
siftDown(vec, 1);
return max;
}