php版快速排序
来源:互联网 发布:centos架设web服务器 编辑:程序博客网 时间:2024/05/18 02:12
今天写了一下php版的插入排序和快速排序,比较一下两者的速度
<?phpclass SortAlgorithm {protected $_data;public function setData($data) {$this->_data = $data;}public function getData() {return $this->_data;}public function isort3() {$x = $this->_data;for ($i=1; $i < count($x); $i++) { $t = $x[$i];for ($j=$i; $j > 0 && $x[$j-1] > $t; $j--) { $x[$j] = $x[$j-1];}$x[$j] = $t;}return $x;}/** * @param [type] $l [description] * @param [type] $u [description] * @param [type] $x [description] * @return [type] [description] */public function qsort_slow($l, $u, $x) {if ($l >= $u) return $x;$m = $l;for ($i=$l+1; $i <= $u; $i++) { if ($x[$i] < $x[$l])list($x[$m], $x[$i]) = array($x[$i], $x[++$m]);}list($x[$l], $x[$m]) = array($x[$m], $x[$l]);$x = $this->qsort_slow($l, $m-1, $x);$x = $this->qsort_slow($m+1, $u, $x);return $x;}}$x = Array();$n = $_SERVER['argv'][1];for($i = 0; $i < $n; $i ++) {$x[] = rand();}$algorithmIns = new SortAlgorithm();$start = time();$algorithmIns->setData($x);$sorted_x = $algorithmIns->isort3();$used = time() - $start;$start = time();echo "isort:{$used}\n";$sorted_x1 = $algorithmIns->qsort_slow(0, $n -1, $x);$used = time() - $start;$start = time();echo "qsort:{$used}\n";
排序1万条数据需要时间分别为:
isort:6
qsort:7
试了几次,快排时间都比插入排序要长
重写了一下,在参数里不传数组:
<?phpclass SortAlgorithm {protected $_data;public function setData($data) {$this->_data = $data;}public function getData() {return $this->_data;}public function isort3() {$x = $this->_data;for ($i=1; $i < count($x); $i++) { $t = $x[$i];for ($j=$i; $j > 0 && $x[$j-1] > $t; $j--) { $x[$j] = $x[$j-1];}$x[$j] = $t;}return $x;}/** * 慢版,由于需要递归地赋值x变量,所以很慢 * @param [type] $l [description] * @param [type] $u [description] * @param [type] $x [description] * @return [type] [description] */public function qsort_slow($l, $u, $x) {if ($l >= $u) return $x;$m = $l;for ($i=$l+1; $i <= $u; $i++) { if ($x[$i] < $x[$l])list($x[$m], $x[$i]) = array($x[$i], $x[++$m]);}list($x[$l], $x[$m]) = array($x[$m], $x[$l]);$x = $this->qsort_slow($l, $m-1, $x);$x = $this->qsort_slow($m+1, $u, $x);return $x;}public function qsort1($l, $u) {if ($l >= $u) return true;$m = $l;for ($i=$l+1; $i <= $u; $i++) { if ($this->_data[$i] < $this->_data[$l])list($this->_data[$m], $this->_data[$i]) = array($this->_data[$i], $this->_data[++$m]);}list($this->_data[$l], $this->_data[$m]) = array($this->_data[$m], $this->_data[$l]);$this->qsort1($l, $m-1);$this->qsort1($m+1, $u);return true;}}$x = Array();$n = $_SERVER['argv'][1];for($i = 0; $i < $n; $i ++) {$x[] = rand();}$algorithmIns = new SortAlgorithm();$start = time();$algorithmIns->setData($x);$sorted_x = $algorithmIns->isort3();$used = time() - $start;$start = time();echo "isort:{$used}\n";$algorithmIns->setData($x);$sorted_x1 = $algorithmIns->qsort1(0, $n -1, $x);$used = time() - $start;$start = time();echo "qsort:{$used}\n";
执行一下,速度立马飙升:
isort:6
qsort:0
0 0
- php版 快速排序
- php版快速排序
- PHP 版快速排序
- PHP快速排序
- php 快速排序算法
- php快速排序
- PHP 快速排序法
- PHP快速查找、排序
- 快速排序php写法
- PHP快速排序
- php快速排序
- PHP 快速排序算法
- php快速排序
- PHP实现快速排序
- php快速排序算法
- 快速排序-php实例
- php快速排序
- php快速排序算法
- 初赛前夕
- Linux下的socket编程实践(三)端口复用和 P2P多进程服务器
- |对自我和人充满了困惑,并试图用掌握的技术手段做出回应。|
- 学习ThinkPHP3.2.2:video15,后台在table中显示许愿
- 门电路
- php版快速排序
- 学习ThinkPHP3.2.2:video15,查询内容按日期排序
- Java 中如何分割字符串
- unity官方demo学习之Stealth(十四)钥匙卡设定
- 两个内部函数体之间的HOOK
- 学习ThinkPHP3.2.2:video15,模板标签中的空格导致输出内容错误
- 移植mysql到tiny6410并附带怎么使6410跑mysql设置
- 剑指offer—把数组排成最小的数
- 文件搜索-压缩解压缩命令