快速排序

2018-04-22  本文已影响8人  软件架构师笔记
<?php

function partion(&$arr, $low, $high)
{
    $privorKey = $arr[$low]; 

    while ($low < $high) {
        while ($low < $high && $arr[$high] >= $privorKey) {
            $high --;   
        }
        $arr[$low] = $arr[$high];

        while ($low < $high && $arr[$low] <= $privorKey) {
            $low ++;
        }
        $arr[$high] = $arr[$low];
    }
    $arr[$low] = $privorKey;

    return $low;
}

function qSort(&$arr, $low, $high) {

    if ($low >= $high) {
        return ;
    }

    $pos = partion($arr, $low, $high);

    qSort($arr, $pos + 1, $high);

    qSort($arr, $low, $pos - 1);
}

$arr = [1,6,7,99,23,4,1,94,5];

qSort($arr, 0, count($arr) - 1);

print_r($arr);


上一篇下一篇

猜你喜欢

热点阅读