-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.php
More file actions
42 lines (31 loc) · 881 Bytes
/
quick_sort.php
File metadata and controls
42 lines (31 loc) · 881 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
<?php
function quick_sort($my_array){
$left_small = $right_big = array();
if(count($my_array) < 2){
return $my_array;
}
$pivot = array_shift($my_array);
$pivot_key = key($my_array);
$n = count($my_array);
for($i = 0;$i < $n;$i++){
if($my_array[$i] < $pivot){
$left_small[] = $my_array[$i];
}else{
$right_big[] = $my_array[$i];
}
}
return array_merge(quick_sort($left_small),array($pivot_key=>$pivot),quick_sort($right_big));
// echo $pivot.'<br>';
// echo '<pre>';
// print_r($left_small);
// echo '</pre>';
// echo '<br>';
// echo '<pre>';
// print_r($right_big);
// echo '</pre>';
}
$my_array = array(6,-2,3,7,8,0,5);
echo 'Mảng trước khi sắp xếp nổi bọt là :'.implode(',',$my_array);
echo '<br>';
echo 'Mảng sau khi sắp xếp nổi bọt là :'.implode(',', quick_sort($my_array));
?>