Skip to content

Instantly share code, notes, and snippets.

@Jason-cqtan
Created September 17, 2019 04:25
Show Gist options
  • Save Jason-cqtan/c92a77384a525431e464a08e901bd3ef to your computer and use it in GitHub Desktop.
Save Jason-cqtan/c92a77384a525431e464a08e901bd3ef to your computer and use it in GitHub Desktop.
php快速排序
function quickSort($array)
{
if(!isset($array[1])) return $array;
$base = $array[0]; //获取一个用于分割的关键字,一般是首个元素
$leftArray = [];
$rightArray = [];
for($i=1; $i<count($array); $i++)
{
if($array[$i] > $base)
$rightArray[] = $array[$i];//把比$base大的数放到一个数组里
else
$leftArray[] = $array[$i];//把比$base小的数放到另一个数组里
}
$leftArray = quickSort($leftArray); //把比较小的数组再一次进行分割
$rightArray = quickSort($rightArray); //把比较大的数组再一次进行分割
return array_merge($leftArray,[$base],$rightArray); //组合两个结果
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment