-
Notifications
You must be signed in to change notification settings - Fork 1
/
9_Template_Heapsort.cc
155 lines (136 loc) · 3.23 KB
/
9_Template_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//模版类Heapsort HeapSort<int> HeapSort<自定义类> 进行堆排序测试
#include <math.h>
#include <iostream>
using std::cout;
using std::endl;
template <typename T >
class HeapSort{
public:
HeapSort(T * arr, int size);
void shift(T r[], int k, int m);
void heapAdjust();
private:
T * & _arr;
int _size;
};
template <typename T >
HeapSort<T>::HeapSort(T * arr, int size)
:_arr(arr)
,_size(size){
for (int i = _size / 2; i > 0; i--) //建堆
shift(_arr, i, _size);
for (int j = _size; j > 0; j--)
{
T temp = _arr[j];
_arr[j] = _arr[1];
_arr[1] = temp;
shift(_arr, 1, j - 1); //当前最大值结点移到数组最后,仅调整 前j-1个元素
}
}
template <typename T >
void HeapSort<T>::shift(T r[], int k, int m)
{//要筛选的节点编号为k,堆中最后一个节点为m
int i, j;
T temp;
i = k;
j = 2*i; //编号为k的节点的左孩子
temp = r[i];//将筛选记录暂存(需要调整的位置)
while(j<=m)//筛选还没进行到的叶子
{
if(j<m && r[j]<r[j+1])//j<m说明i节点还有一个右孩子,为二度节点
{
j++;//i有两个孩子,取大孩子
}
//一个孩子或者左孩子比右孩子大
if(r[i] > r[j])
{
break;//孩子比父亲小
}
else//孩子比父亲大
{
r[i] = r[j];//将较大的节点移到根上。
i = j;
j = 2*i;
//将i指向交换的位置,为进一步移动做准备。
}
r[i] = temp;//将筛选记录移到正确位置
}
}
//***********************************************************Point类型
class Point
{
public:
/* //提供无参(参数有默认初始值)的构造函数,以免template初始化error
Point()
:_ix(0)
,_iy(0){}
*/
Point(int ix=0, int iy=0)
: _ix(ix)
, _iy(iy)
{}
friend std::ostream & operator<<(std::ostream & os, const Point &rhs);
friend bool operator<(const Point & lhs, const Point & rhs);
friend bool operator>(const Point & lhs, const Point & rhs);
float distance() const
{
return sqrt(_ix * _ix + _iy * _iy);
}
private:
int _ix;
int _iy;
};
std::ostream & operator<<(std::ostream & os, const Point &rhs)
{
os << "(" << rhs._ix
<< "," << rhs._iy
<< ")";
return os;
}
bool operator<(const Point & lhs, const Point & rhs)
{
return lhs.distance() < rhs.distance();
}
bool operator>(const Point & lhs, const Point & rhs)
{
return lhs.distance() > rhs.distance();
}
void test_int(){
int size=10;
int a[] = {0,4,5,8,2,1,6,7,9,10,3}; //a[0]不参与排序
cout << "大顶堆排序前的序列:" << endl;
for (int i = 1; i <= size; i++)
{
cout << a[i] << " ";
}
HeapSort<int>(a, size);
cout<<endl;
cout << "大顶堆排序后的序列:" << endl;
for (int i = 1; i <= size; i++)
{
cout << a[i] << " ";
}
cout<<endl;
}
void test_Point(){
int size=10;
Point a[] = {Point(0,0),Point(1,1),Point(2,2),Point(-1,-1),Point(-2,2),Point(3,3),Point(5,5),Point(7,7),Point(4,4),Point(6,6),Point(0,1)}; //a[0]不参与排序
cout << "大顶堆排序前的序列:" << endl;
for (int i = 1; i <= size; i++)
{
cout << a[i] << " ";
}
HeapSort<Point>(a, size);
cout<<endl;
cout << "大顶堆排序后的序列:" << endl;
for (int i = 1; i <= size; i++)
{
cout << a[i] << " ";
}
cout<<endl;
}
int main(){
//test_int();
test_Point();
return 0;
}