-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSort.php
More file actions
52 lines (48 loc) · 907 Bytes
/
shellSort.php
File metadata and controls
52 lines (48 loc) · 907 Bytes
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
<?php
/**
* 希尔排序
*
* @param array $arr
* @return array
*/
function shellSort(array $arr)
{
for ($gap = floor(count($arr) / 2); $gap > 0; $gap = floor($gap / 2)) { // 缩小增量
for ($i = $gap; $i < count($arr); $i ++) { // 组内循环排序
$j = $i;
while ($j - $gap >= 0 && $arr[$j] < $arr[$j - $gap]) { // 完成组内元素一次排序
swap($arr, $j, $j - $gap);
}
}
echo implode(',', $arr) . PHP_EOL; // 完成一次增量输出一次结果
}
return $arr;
}
/**
* 交换函数
*
* @param array $arr
* @param int $a
* @param int $b
*/
function swap(array &$arr, int $a, int $b)
{
$temp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $temp;
}
$arr = array(
2,
13,
42,
34,
56,
23,
67,
365,
87665,
54,
68,
3
);
print_r(shellSort($arr));