Skip to content

Commit 6710255

Browse files
Merge pull request matthewsamuel95#805 from UmmeAmmaraa/add-amara
Add Umme Ammara to Contributors list
2 parents 8316a86 + 8c47513 commit 6710255

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Sorting/QuickSort/PHP/Quicksort.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
$unsorted = array(43,21,2,1,9,24,2,99,23,8,7,114,92,5);
2+
3+
function quick_sort($array)
4+
{
5+
// find array size
6+
$length = count($array);
7+
8+
// base case test, if array of length 0 then just return array to caller
9+
if($length <= 1){
10+
return $array;
11+
}
12+
else{
13+
14+
// select an item to act as our pivot point, since list is unsorted first position is easiest
15+
$pivot = $array[0];
16+
17+
// declare our two arrays to act as partitions
18+
$left = $right = array();
19+
20+
// loop and compare each item in the array to the pivot value, place item in appropriate partition
21+
for($i = 1; $i < count($array); $i++)
22+
{
23+
if($array[$i] < $pivot){
24+
$left[] = $array[$i];
25+
}
26+
else{
27+
$right[] = $array[$i];
28+
}
29+
}
30+
31+
// use recursion to now sort the left and right lists
32+
return array_merge(quick_sort($left), array($pivot), quick_sort($right));
33+
}
34+
}
35+
36+
$sorted = quick_sort($unsorted);
37+
print_r($sorted);

0 commit comments

Comments
 (0)