Skip to content

Instantly share code, notes, and snippets.

@firomero
Created June 1, 2015 03:34
Show Gist options
  • Save firomero/e3417619e6e2d70a30d7 to your computer and use it in GitHub Desktop.
Save firomero/e3417619e6e2d70a30d7 to your computer and use it in GitHub Desktop.
quickSort
<?php
function quicksort( $array ) {
if( count( $array ) < 2 ) {
return $array;
}
$left = $right = array( );
reset( $array );
$pivot_key = key( $array );
$pivot = array_shift( $array );
foreach( $array as $k => $v ) {
if( $v < $pivot )
$left[$k] = $v;
else
$right[$k] = $v;
}
return array_merge(quicksort($left), array($pivot_key => $pivot), quicksort($right));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment