-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.cpp
More file actions
69 lines (54 loc) · 1.78 KB
/
HeapSort.cpp
File metadata and controls
69 lines (54 loc) · 1.78 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
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
const int N = 1e6;
double a[N+5];
void heapify(int n, int i) {
/// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
largest = left;
if (right < n && a[right] > a[largest])
largest = right;
/// Swap and continue heapifying if root is not largest
if (largest != i) {
swap(a[i], a[largest]);
heapify(n, largest);
}
}
void heapSort(int n){
/// Build max heap
for(int i = n / 2 - 1; i >= 0; i--)
heapify(n, i);
/// Heap sort
for(int i = n - 1; i >= 0; i--){
swap(a[0], a[i]);
/// Heapify root element to get highest element at root again
heapify(i, 0);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
/// freopen("HeapSortResult.out", "w", stdout);
for(int i = 1; i <= 10; ++i){
string filename = "TEST/test";
filename = filename + to_string(i) + ".inp";
const char* filename_cstr = filename.c_str();
freopen(filename_cstr, "r", stdin);
for(int j = 0; j < N; ++j)cin >> a[j];
/// calculate time
auto start = chrono::high_resolution_clock::now();
heapSort(N);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> elapsed = end - start;
/// check for sorted
for(int j = 1; j < N; ++j)if(a[j-1] > a[j]){
cout << fixed << setprecision(6) << j << ' ' << a[j-1] << ' ' << a[j] << endl;
return 0;
}
cout << "Test " << i << ": " << elapsed.count() << " ms" << endl;
}
}