php版快速排序

今天写了一下php版的插入排序和快速排序,比较一下两者的速度

<?php
class 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

试了几次,快排时间都比插入排序要长

重写了一下,在参数里不传数组:


<?php
class 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值