forked from codesession/Data-structure-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.cpp
More file actions
34 lines (30 loc) · 629 Bytes
/
heapsort.cpp
File metadata and controls
34 lines (30 loc) · 629 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
#include"heap.h"
typedef function<bool(int, int)> compare;
void heapsort(vector<int>& vec, compare comp = less<int>()) //只接受vector容器,默认从小到大排序
{
Heap myheap(vec, comp);
vec.clear();
while (myheap.size() > 0)
{
vec.push_back(myheap.removeroot());
}
}
int main(int argc, char const *argv[])
{
//1 2 2 3 3 5 5 7 8
vector<int> myvec = {8, 5, 3, 2, 1, 5, 7, 2, 3};
heapsort(myvec);
for (auto i : myvec)
{
std::cout << i << " ";
}
std::cout << endl;
//8 7 5 5 3 3 2 2 1
heapsort(myvec, greater<int>());
for (auto i : myvec)
{
std::cout << i << " ";
}
std::cout << endl;
return 0;
}