-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
executable file
·70 lines (64 loc) · 1.84 KB
/
heap_sort.cpp
File metadata and controls
executable file
·70 lines (64 loc) · 1.84 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
#include <iostream>
#include <vector>
#include <ctime>
#include <iterator>
#include <algorithm>
#include "io.hpp"
template <class T>
void sift_down(std::vector<T>& v, unsigned start, unsigned end)
{
unsigned root = start;
unsigned left_child = 2 * root + 1;
while (left_child < end) {
unsigned greatest = root;
// Find the greatest among parent, 1st child and 2nd child
if (v[left_child] > v[root]) {
greatest = left_child;
}
if (left_child + 1 < end && v[left_child + 1] > v[greatest]) {
greatest = left_child + 1;
}
if (greatest == root) {
return;
} else {
std::swap(v[root], v[greatest]);
root = greatest;
left_child = 2 * root + 1;
}
}
}
template <class T>
void heapify(std::vector<T>& v)
{
unsigned last_parent = (v.size() - 2) / 2;
for (int i = last_parent; i >= 0; --i) {
sift_down(v, i, v.size());
}
}
template <class T>
void heap_sort(std::vector<T>& v)
{
heapify(v);
unsigned last = v.size() - 1;
while (last > 0) {
std::swap(v[0], v[last]);
sift_down(v, 0, last);
--last;
}
}
int main(int argc, char* argv[])
{
std::string source_file("unsorted.bin");
if (argc > 1) {
source_file = argv[1];
std::cout << "Reading data from " << source_file << std::endl;
}
auto data = read_values<unsigned>(source_file.c_str());
clock_t t0 = clock();
heap_sort<unsigned>(data);
clock_t t1 = clock();
double spent = 1000 * static_cast<double>(t1 - t0) / CLOCKS_PER_SEC;
std::cout << "Array size: " << data.size() << std::endl;
std::cout << "Time spent: " << spent << " ms" << std::endl;
write_values<unsigned>("heap_sort.sorted", data);
}