-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path快速排序.php
More file actions
35 lines (32 loc) · 680 Bytes
/
快速排序.php
File metadata and controls
35 lines (32 loc) · 680 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
<?php
/**
* Name: 快速排序
*/
$array = [5,1,4,6,2,10,3,7,8,9];
function run(&$array)
{
if(count($array) > 1)
{
$array_count = count($array);
$key = $array[0];
$left = [];
$right = [];
for ($i=1;$i<$array_count;$i++)
{
if($key > $array[$i])
{
$left[] = $array[$i];
}
else if($key <= $array[$i])
{
$right[] = $array[$i];
}
}
$left = run($left);
$right = run($right);
return array_merge($left,array($key),$right);
}else{
return $array;
}
}
var_dump(run($array));