中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

PHP一個(gè)簡(jiǎn)單的快速排序

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
通過不斷的定位基準(zhǔn)數(shù)的位置來實(shí)現(xiàn)快速排序
<?php
/**
 * Created by PhpStorm.
 * User: saint
 * Date: 15/8/5
 * Time: 上午11:49
 */
class Demo
{
    public $a = array(3, 6, 9, 2, 4, 7, 1, 5, 8, 0);
 
    public function qsort($left, $right)
    {
        if($left > $right) {
            return;
        }
 
        $i = $left;
        $j = $right;
        $standard = $this->a[$left];
 
        while($i != $j) {
 
            // 從右向左查找比基準(zhǔn)數(shù)小的單元
            while(($standard <= $this->a[$j]) && ($j > $i)) {
                $j--;
            }
 
            // 從左到右查找比基準(zhǔn)數(shù)大的
            while(($standard >= $this->a[$i]) && ($j > $i)) {
                $i++;
            }
 
            $tmp = $this->a[$i];
            $this->a[$i] = $this->a[$j];
            $this->a[$j] = $tmp;
        }
 
        // 確定基準(zhǔn)數(shù)的位置
        $this->a[$left] = $this->a[$i];
        $this->a[$i] = $standard;
 
        $this->qsort($left, $i - 1);
        $this->qsort($i + 1, $right);
    }
 
    // 執(zhí)行函數(shù)
    public function main()
    {
        $left = 0;
        $right = count($this->a) - 1;
        $this->qsort($left, $right);
        print_r($this->a);
    }
}
 
$demo = new Demo();
$demo->main();

來自:http://my.oschina.net/liuke1556/blog/488215

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:獲取指定日期的隨機(jī)時(shí)間的Oracle函數(shù)

下一篇:Python模擬鍵盤輸入和鼠標(biāo)操作