首頁 >php教程 >PHP源码 >递归与迭代同时实现php快速排序

递归与迭代同时实现php快速排序

PHP中文网
PHP中文网原創
2016-05-25 17:05:341138瀏覽

php代码

/**
 * 递归法实现的快速排序
 * @param $seq
 * @return array
 */
function quicksort($seq)
{
    if (count($seq) > 1) {
        $k = $seq[0];
        $x = array();
        $y = array();
        $_size = count($seq); //do not use count($seq) in loop for.
        for ($i = 1; $i < $_size; $i++) {
            if ($seq[$i] <= $k) {
                $x[] = $seq[$i];
            } else {
                $y[] = $seq[$i];
            }
        }
        $x = quicksort($x);
        $y = quicksort($y);
        return array_merge($x, array($k), $y);
    } else {
        return $seq;
    }
}

/**用迭代实现
 * @param $seq
 * @return array
 */
function quicksortX(&$seq)
{
    $stack = array($seq);
    $sort = array();
    while ($stack) {
        $arr = array_pop($stack);
        if(count($arr) <= 1){
            if (count($arr) == 1) {
                $sort[] = &$arr[0];
            }
            continue;
        }

        $k = $arr[0];
        $x = array();
        $y = array();
        $_size = count($arr);
        for ($i = 1; $i < $_size; $i++) {
            if ($arr[$i] <= $k) {
                $x[] = &$arr[$i];
            } else {
                $y[] = &$arr[$i];
            }
        }
        !empty($y) && array_push($stack, $y);
        array_push($stack, array($arr[0]));
        !empty($x) && array_push($stack, $x);
    }
    return $sort;
}
//$testArr = array(5545, 5, 6, 7675, 100, 9, 233, 566, 789, 456, 23, 55, 7, 4, 343, 564, 5, 45657, 8, 998, 9, 34, 34, 55, 6, 5, 6433, 67, 6, 6766, 4, 2, 42, 25634, 34343, 3, 3, 454, 4, 65, 6678, 57, 5455);
for($i=0;$i<20;$i++){
    $testArr[]=mt_rand(0,10000);
}
//var_dump($testArr);
echo count($testArr).&#39;
&#39;;
$start=microtime();
echo count(quicksort($testArr)).&#39;
&#39;;
echo microtime()-$start.&#39;
&#39;;
var_dump(quicksort($testArr));
echo &#39;------------------------------------------------------------------------
&#39;;
echo count($testArr).&#39;
&#39;;
$start=microtime();
echo count(quicksortX($testArr)).&#39;
&#39;;
echo microtime()-$start.&#39;
&#39;;
var_dump(quicksortX($testArr));

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn