Skip to content

Instantly share code, notes, and snippets.

@polamayster
Last active August 29, 2015 14:09
Show Gist options
  • Save polamayster/feaccb6580a2cea304a2 to your computer and use it in GitHub Desktop.
Save polamayster/feaccb6580a2cea304a2 to your computer and use it in GitHub Desktop.
<?php
$a = array(1,2,5,9,8,7,4,5,6,5,8,15,10,25,85,4,1,2,78,9);
function qsort(array &$a, $low, $high)
{
if ($low < $high){
$pivot = partition($a, $low, $high);
qsort($a, $low, $pivot - 1);
qsort($a, $pivot + 1, $high);
}
}
function partition(array &$a, $left, $right)
{
$pivot = (int)($left + $right)/2;
$pivVal = $a[$pivot];
swap($a, $pivot, $right);
$index = $left;
for ($i = $left; $i < $right; $i++) {
if ($a[$i] < $pivVal) {
swap($a, $i, $index++);
}
}
swap($a, $index, $right);
return $index;
}
function swap(array &$a, $i, $j)
{
$swap = $a[$i];
$a[$i] = $a[$j];
$a[$j] = $swap;
}
print_r($a);
qsort($a, 0, count($a)-1);
print_r($a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment