-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHeapSort.php
More file actions
259 lines (219 loc) · 6.13 KB
/
HeapSort.php
File metadata and controls
259 lines (219 loc) · 6.13 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
function pre($arr)
{
$data = func_get_args();
foreach($data as $key=>$val)
{
echo '<pre>';
print_r($val);
echo '</pre>';
}
}
function prend()
{
$data = func_get_args();
foreach($data as $key=>$val)
{
echo '<pre>';
print_r($val);
echo '</pre>';
}
exit();
}
/**
*
* 构建大堆序
*
* @author yumancang
*
*/
class HeapSort {
public $data;
public function __construct(Array $array)
{
$this->data = $array;
}
/**
* 将数组初始化构建堆
* 从最后一个非叶子节点开始,递归到第0个, index = n/2 -1
*
*/
public function buildMaxHeap()
{
$noLeafNodeIndex = floor(count($this->data)/2) - 1;
for ($i = $noLeafNodeIndex; $i>=0; $i--) {
$this->adjustMaxHeap($i,count($this->data));
}
pre("构建后",$this->data);
//prend($noLeafNodeIndex);
}
/**
* 交换头尾,在构建堆
*
*/
public function sortMaxHeap()
{
$k = 1;
for ($i = count($this->data) - 1; $i > 0 ; $i--) {
//每次构建好堆后,交换头尾
$temp = $this->data[$i];
$this->data[$i] = $this->data[0];
$this->data[0] = $temp;
pre("第".$k."趟,交换头尾后",$this->data);
if ($k == 3) {
$this->adjustMaxHeap(0,$i);
}
//交换头尾后,按照堆规则,重新构建堆
$this->adjustMaxHeap(0,$i);
pre("第".$k."趟,构建完堆后",$this->data);
$k++;
}
}
/**
* 按照堆规则,重新构建堆
*
* @param int $parentIndex
* @param int $length
*/
public function adjustMaxHeap(int $parentIndex,int $length)
{
$childIndex = 2*$parentIndex + 1;
while ($childIndex < $length) {
if ($childIndex+1 < $length && isset($this->data[$childIndex+1]) && $this->data[$childIndex+1] > $this->data[$childIndex]) {
$childIndex++;
}
if ($this->data[$parentIndex] >= $this->data[$childIndex]) {
break;
}
$temp = $this->data[$parentIndex];
$this->data[$parentIndex] = $this->data[$childIndex];
$this->data[$childIndex] = $temp;
$parentIndex = $childIndex;
$childIndex = 2*$parentIndex + 1;
}
}
/**
* 将数组初始化构建堆
* 从最后一个非叶子节点开始,递归到第0个, index = n/2 -1
*
*/
public function buildMinHeap()
{
$noLeafNodeIndex = floor(count($this->data)/2) - 1;
for ($i = $noLeafNodeIndex; $i>=0; $i--) {
$this->adjustMinHeap($i,count($this->data));
}
pre("构建后",$this->data);
//prend($noLeafNodeIndex);
}
/**
* 交换头尾,在构建堆
*
*/
public function sortMinHeap()
{
$k = 1;
for ($i = count($this->data) - 1; $i > 0 ; $i--) {
//每次构建好堆后,交换头尾
$temp = $this->data[$i];
$this->data[$i] = $this->data[0];
$this->data[0] = $temp;
pre("第".$k."趟,交换头尾后",$this->data);
if ($k == 3) {
$this->adjustMinHeap(0,$i);
}
//交换头尾后,按照堆规则,重新构建堆
$this->adjustMinHeap(0,$i);
pre("第".$k."趟,构建完堆后",$this->data);
$k++;
}
}
/**
* 按照堆规则,重新构建堆
*
* @param int $parentIndex
* @param int $length
*/
public function adjustMinHeap(int $parentIndex,int $length)
{
$childIndex = 2*$parentIndex + 1;
while ($childIndex < $length) {
if ($childIndex+1 < $length && isset($this->data[$childIndex+1]) && $this->data[$childIndex+1] < $this->data[$childIndex]) {
$childIndex++;
}
if ($this->data[$parentIndex] <= $this->data[$childIndex]) {
break;
}
$temp = $this->data[$parentIndex];
$this->data[$parentIndex] = $this->data[$childIndex];
$this->data[$childIndex] = $temp;
$parentIndex = $childIndex;
$childIndex = 2*$parentIndex + 1;
}
}
/**
* 大堆
*
*/
public function maxHeap()
{
//pre("初始数组",$heapSort->data);
$this->buildMaxHeap();
$this->sortMaxHeap();
//pre("已排序数组",$heapSort->data);
}
/**
* 小堆
*
*/
public function minHeap()
{
//pre("初始数组",$heapSort->data);
$this->buildMinHeap();
$this->sortMinHeap();
//pre("已排序数组",$heapSort->data);
}
}
//$data = [20,50,10,30,70,20,80,33,44,55,66];
//[20,50,10,30,70,20,80]
/**
* 100个数字里面求最大的10个数字,采用小堆,小值往上移 (n-m) * logn
*
*/
/* $data = [];
for ($i = 0; $i<100;$i++) {
$data[$i] = rand(1,1000);
}
pre($data);
$initData = array_slice($data, 0,10);
$heapSort = new HeapSort($initData);
$heapSort->buildMinHeap();
for ($i = 10;$i<count($data); $i++) {
if ($data[$i] > $heapSort->data[0]) {
$heapSort->data[0] = $data[$i];
$heapSort->adjustMinHeap(0, 10);
}
}
prend(sort($heapSort->data),$heapSort->data);
*/
/**
*
* 100个数字里面求最小的10个数字,采用大堆,大值往上移 (n-m) * logn
*
*/
$data = [];
for ($i = 0; $i<100;$i++) {
$data[$i] = rand(1,1000);
}
pre($data);
$initData = array_slice($data, 0,10);
$heapSort = new HeapSort($initData);
$heapSort->buildMaxHeap();
for ($i = 10;$i<count($data); $i++) {
if ($data[$i] < $heapSort->data[0]) {
$heapSort->data[0] = $data[$i];
$heapSort->adjustMaxHeap(0, 10);
}
}
prend(sort($heapSort->data),$heapSort->data);
?>