Created
June 1, 2015 03:34
-
-
Save firomero/e3417619e6e2d70a30d7 to your computer and use it in GitHub Desktop.
quickSort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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