Created
March 5, 2019 08:35
-
-
Save wlight/23a75ba685ba55bb7cab62b720425e09 to your computer and use it in GitHub Desktop.
快速排序
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) <= 1){ | |
return $array; | |
} | |
$mid = $array[0]; | |
$right_array = array(); | |
$left_array = array(); | |
for($i = 1;$i < count($array); $i++) { | |
if($array[$i] > $mid) { | |
$right_array[] = $array[$i]; | |
} else { | |
$left_array[] = $array[$i]; | |
} | |
} | |
$right_array = quickSort($right_array); | |
$left_array = quickSort($left_array); | |
$array = array_merge($left_array, array($mid), $right_array); | |
return $array; | |
} | |
$num = [8,4,5,0,3,4,2]; | |
var_dump(quickSort($num)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment