-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeapSort.cpp
103 lines (83 loc) · 1.64 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
// index start from 1
void MaxHeapify(vector<int> & vec, int index, int n)
{
if (0 == vec.size() || n < index)
{
return;
}
int value = vec[index];
while(2 * index <= n)
{
int j = 2 * index;
if (j + 1 <= n && vec[j + 1] > vec[j])
{
++j;
}
if (vec[j] < value)
{
break;
}
vec[index] = vec[j];
index = j;
}
vec[index] = value;
}
// index start from 1
void BuildHeap(vector<int> & vec)
{
int n = vec.size();
for (int i = n / 2; 0 < i; --i)
{
MaxHeapify(vec, i, n);
}
}
// index start from 0
void HeapSort(vector<int> & vec)
{
if (vec.empty())
{
return;
}
int n = vec.size();
vector<int> temp(n+1, 0);
memcpy(&temp[1], &vec[0], sizeof(int) * n);
BuildHeap(temp);
for (int i = n; 1 < i; --i)
{
swap(temp[1], temp[i]);
MaxHeapify(temp, 1, i-1);
}
memcpy(&vec[0], &temp[1], sizeof(int) * n);
}
int main()
{
vector<int> vec;
vec.reserve(128);
for (int i = 10; 0 < i; --i)
{
for (int j = 2; 0 < j; --j)
{
vec.push_back(i);
}
}
vector<int> vecHeap(vec), vecSTL(vec);
sort(vecSTL.begin(), vecSTL.end());
HeapSort(vecHeap);
cout << "STL sort" << endl;
for (int item : vecSTL)
{
cout << item << " ";
}
cout << endl;
cout << endl << "Heap sort" << endl;
for (int item : vecHeap)
{
cout << item << " ";
}
cout << endl;
}