-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort_withPrint
66 lines (63 loc) · 1.19 KB
/
QuickSort_withPrint
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
55
56
57
58
59
60
61
62
63
64
65
66
<?php
class QuickSort {
public function quickSort($arr, $left, $right) {
$i = $left;
$j = $right;
$pivot = $arr[floor(($left + $right) / 2)];
print "<br>START<br>";
print "i = ";
print_r($i);
print "<br>";
print "j = ";
print_r($j);
print "<br>";
print "Pivot = ";
print_r($pivot);
print "<br>";
while ($i <= $j) {
while ($arr[$i] < $pivot) {
$i++;
}
print "New i = ";
print_r($i);
print "<br>";
while ($arr[$j] > $pivot) {
$j--;
}
print "New j = ";
print_r($j);
print "<br>";
print "Old Arr = ";
print_r(join($arr, ", "));
print "<br>";
if ($i <= $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
$i++;
$j--;
}
print "New Arr = ";
print_r(join($arr, ", "));
print "<br>";
}
print "Left = ";
print_r($left);
print "<br>";
if ($left < $j) {
$arr = $this->quickSort($arr, $left, $j);
}
print "Right = ";
print_r($right);
print "<br>";
if ($right > $i) {
$arr = $this->quickSort($arr, $i, $right);
}
return $arr;
}
}
$arr = array(20,12,4,13,5,8,12,15,45,75);
$sort = new QuickSort();
echo "Sorted array<br>";
print_r($sort->quickSort($arr, 0, count($arr) -1));
?>