手记

php 快速排序

<?php

//快速排序
function quickSort(array $arr) {
    $len = count($arr);
    if ($len <= 1) return $arr;
    $arr1 = $arr2 = array();
    $key = $arr[0];
    for ($i = 1; $i <= $len-1; $i++) {
        if ($arr[$i] <= $key) {
            $arr1[] = $arr[$i];
        } else {
            $arr2[] = $arr[$i];
        }
    }
    $arr1 = quickSort($arr1);
    $arr2 = quickSort($arr2);
    return array_merge($arr1, (array)$key, $arr2);
}
$str=array(2,3,212,211,4,61,78,6,65,45,42,23,421,96,755,454);
echo '<pre>';
print_r(quickSort($str));
exit;


?>

0人推荐
随时随地看视频
慕课网APP