-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.php
More file actions
29 lines (27 loc) · 786 Bytes
/
quickSort.php
File metadata and controls
29 lines (27 loc) · 786 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
<?php
function quickSort(&$arr){
if(count($arr)>1){
$k=$arr[0];
$x=array();
$y=array();
$_size=count($arr);
for($i=1;$i<$_size;$i++){
if($arr[$i]<=$k){
$x[]=$arr[$i];
}else{
$y[]=$arr[$i];
}
}
echo implode(', ',$x). PHP_EOL;
echo implode(', ',$y). PHP_EOL;
$x=quickSort($x);
$y=quickSort($y);
return array_merge($x,array($k),$y);
}else{
return $arr;
}
}
$test_array = array(49, 40, 60, 80, 30, 70, 90, 10, 50, 0);
echo implode(', ',quickSort($test_array)). PHP_EOL;
//https://codereview.stackexchange.com/questions/82834/quick-sort-in-php
//https://pageconfig.com/post/implementing-quicksort-in-php