-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSort.cpp
More file actions
41 lines (38 loc) · 1000 Bytes
/
heapSort.cpp
File metadata and controls
41 lines (38 loc) · 1000 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
void heapAdjustArray(std::vector<int> &vec, int nParent, int nLimit) {
for(int nChild = 2 * nParent + 1; nChild < nLimit; nParent = nChild) {
if(nChild + 1 < nLimit && vec[nChild + 1] > vec[nChild]) ++nChild;
if(vec[nParent] < vec[nChild])
std::swap(vec[nParent], vec[nChild]);
else
break;
}
}
void heapSortArray(std::vector<int> &vec) {
int len = vec.size();
for(int i = (len - 1) / 2; i >=0; --i)
heapAdjustArray(vec, i, len);
for(int i = len - 1; i > 0; --i) {
std::swap(vec[0], vec[i]);
heapAdjustArray(vec, 0, i);
}
}
int main() {
freopen("in.txt", "r", stdin);
int k;
while(std::cin>>k) {
std::vector<int> vec(k);
for(int i = 0; i < k; ++i)
std::cin>>vec[i];
for(int i = 0; i < k; ++i)
std::cout << vec[i] << ' ';
std::cout<<std::endl;
heapSortArray(vec);
std::cout<<"after heapSort"<<std::endl;
for(int i = 0; i < k; ++i)
std::cout << vec[i] << ' ';
std::cout<<std::endl;
}
}