-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.cpp
57 lines (47 loc) · 1.08 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
#include <vector>
#include <iostream>
using namespace std;
// 维护堆的性质
// 维护下标为i的节点为大根堆
void heapify(int A[], int n, int i) {
int largest = i;
int lson = i * 2 + 1;
int rson = i * 2 + 2;
if (lson < n && A[lson] > A[largest])
largest = lson;
if (rson < n && A[rson] > A[largest])
largest = rson;
// 节点发生改变
if (largest != i) {
swap(A[i], A[largest]);
heapify(A, n, largest);
}
}
void HeapSort(int A[], int n)
{
// 建堆
for (int i = n / 2 - 1; i >= 0; i--)
heapify(A, n, i);
// 排序
for (int i = n - 1; i > 0; i--) {
swap(A[i], A[0]);
heapify(A, i, 0);
}
}
void printNums(int nums[], int n)
{
for (int i = 0; i < n; i++)
cout << nums[i] << " ";
cout << endl;
}
int main()
{
int test[12] = {4, 2, 7, 10, 3, 9, 100, 64, 77, 92, 101, 50};
int n = sizeof(test) / sizeof(test[0]);
// 排序前打印
printNums(test, n);
HeapSort(test, n);
// 排序后打印
printNums(test, n);
return 0;
}