Skip to content

Instantly share code, notes, and snippets.

@lucian303
Last active August 29, 2015 13:55
Show Gist options
  • Save lucian303/8699751 to your computer and use it in GitHub Desktop.
Save lucian303/8699751 to your computer and use it in GitHub Desktop.
<?php
/**
* A simple quicksort implementation for exercise purposes
*/
function quicksort($list)
{
$listLength = count($list);
if ($listLength <= 1) {
return $list;
}
$pivot = floor($listLength / 2);
$pivotValue = $list[$pivot];
$lesser = $greater = array();
foreach ($list as $value) {
if ($value <= $pivotValue) {
array_push($lesser, $value);
}
else {
array_push($greater, $value);
}
}
return array_merge(quicksort($lesser), quicksort($greater));
}
$array = [20, 5, 10, 1, 4, 3, 2];
print_r($array);
print_r(quicksort($array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment