-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.php
More file actions
54 lines (49 loc) · 1022 Bytes
/
quickSort.php
File metadata and controls
54 lines (49 loc) · 1022 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
53
54
<?php
/**
* 快速排序
*
* @param array $arrayrray
* @return unknown|array
*/
function quickSort($array)
{
// 判断是否需要运行,因下面已拿出一个中间值,这里<=1
if (count($array) <= 1) {
return $array;
}
$middle = $array[0]; // 中间值
$left = array(); // 接收小于中间值
$right = array(); // 接收大于中间值
// 循环比较
for ($i = 1; $i < count($array); $i ++) {
if ($middle < $array[$i]) {
// 大于中间值
$right[] = $array[$i];
} else {
// 小于中间值
$left[] = $array[$i];
}
}
// 递归排序划分好的2边
$left = quickSort($left);
$right = quickSort($right);
// 合并排序后的数据,别忘了合并中间值
return array_merge($left, array(
$middle
), $right);
}
$arr = array(
2,
13,
42,
34,
56,
23,
67,
365,
87665,
54,
68,
3
);
print_r(quickSort($arr));