加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP一个简单的快速排序

发布时间:2020-05-25 07:37:10 所属栏目:PHP 来源:互联网
导读:PHP一个简单的快速排序

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

通过不断的定位基准数的位置来实现快速排序
<?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) {
 
            // 从右向左查找比基准数小的单元
            while(($standard <= $this->a[$j]) && ($j > $i)) {
                $j--;
            }
 
            // 从左到右查找比基准数大的
            while(($standard >= $this->a[$i]) && ($j > $i)) {
                $i++;
            }
 
            $tmp = $this->a[$i];
            $this->a[$i] = $this->a[$j];
            $this->a[$j] = $tmp;
        }
 
        // 确定基准数的位置
        $this->a[$left] = $this->a[$i];
        $this->a[$i] = $standard;
 
        $this->qsort($left,$i - 1);
        $this->qsort($i + 1,$right);
    }
 
    // 执行函数
    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

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读