-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheapSort.cc
79 lines (75 loc) · 2.1 KB
/
heapSort.cc
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
///
/// @file HeapSort.cc
/// @author yll(1711019653@qq.com)
/// @date 2019-01-23 19:55:48
///
#include <iostream>
#include<array>
using std::cout;
using std::endl;
using std::array;
class HeapSort
{
public:
//堆排序
static void heapSort(int numbers[] , int last)
{
buildHeap(numbers, last); //建大顶堆
for(int index_last = last ; index_last >= 1; --index_last)
{
swap(numbers[0],numbers[index_last]);
//cout << " index_last" << index_last << endl;
adjustHeap(numbers, index_last - 1, 0);//出堆后。向下调整
}
}
// swap();
static void swap(int & x, int & y)
{
int temp = x;
x = y;
y = temp;
}
//建大顶堆
static void buildHeap(int numbers[], int last)
{
for(int i = (last-1)/2; i >= 0; --i)
{
adjustHeap(numbers, last, i);
}
}
//调整
static void adjustHeap(int numbers[], int last, int index)
{
int maxidx = index; //存放父节点,左右孩子节点三者最大的下标值
int index_lchild = index*2 + 1;
int index_rchild = index*2 + 2; //从零开始的规律,与二叉树从1开始有所区别
if(index_lchild <= last && numbers[index_lchild] > numbers[maxidx]) maxidx = index_lchild;
if(index_rchild <= last && numbers[index_rchild] > numbers[maxidx]) maxidx = index_rchild; //得到最大值的下标
if(maxidx != index)
{
swap(numbers[index], numbers[maxidx]);
adjustHeap(numbers, last, maxidx); //往下继续调整成大顶堆
}
}
// if(index_rchild <= last)
// {
// if(numbers[index_lchild] > numbers[index_rchild] && numbers[index_lchild] > numbers[i])
// {
// swap(numbers[i], numbers[index_lchild]);
//
// }
// else if(numbers[index_rchild] >= numbers[index_lchild] && numbers[index_rchild] > numbers[i])
// swap(numbers[i], numbers[index_rchild]);
// }
// else if(numbers[index_lchild] > numbers[i] && index_lchild <= last && index_rchild > last)
// swap(numbers[i], numbers[index_lchild]);
};
int main(void)
{
int array[15] = {5,2,8,12,6,9,8,3,1,15,10,11,7,14,13};
HeapSort::heapSort(array, 14);
for(int index = 0 ; index < 15; ++index)
cout << array[index] << " " ;
cout << endl;
return 0;
}