Skip to content

Instantly share code, notes, and snippets.

@wlight
Created March 5, 2019 08:35
Show Gist options
  • Save wlight/23a75ba685ba55bb7cab62b720425e09 to your computer and use it in GitHub Desktop.
Save wlight/23a75ba685ba55bb7cab62b720425e09 to your computer and use it in GitHub Desktop.
快速排序
<?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