Home > Backend Development > PHP Tutorial > Quick sort Qsort

Quick sort Qsort

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:22:04
Original
1254 people have browsed it
<?php
function Partition(&$arr,$low,$high)
{
	$_t=$arr[$low];
	while($low<$high)
	{
		//注意第二个限制条件,不然如果一直都是大于会超过数组下标
		while($arr[$high]>$_t&&$high>$low)  --$high;
		$arr[$low]=$arr[$high];
		while($arr[$low]<$_t&&$low<$high)  ++$low;
		$arr[$high]=$arr[$low];
	}
	$arr[$low]=$_t;
	return $low;
}
function Qsort(&$arr,$low,$high)
{
	if($low<$high)
	{
		$mid=Partition($arr,$low,$high);
		Qsort($arr,$low,$mid-1);
		Qsort($arr,$mid+1,$high);
	}
}
$a=array(9,8,7,10,11,4,3,2,1);
Qsort($a,0,8);
var_dump($a);
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced quick sorting Qsort, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template