search
Homephp教程php手册php实现快速排序法函数代码

php实现快速排序法函数代码

Jun 13, 2016 am 11:58 AM
functionphpstrcodefunctioncopyaccomplishfastsort

代码1:

复制代码 代码如下:


function quicksort($str){
if(count($str)$key=$str[0];//取一个值,稍后用来比较;
$left_arr=array();
$right_arr=array();
for($i=1;$iif($str[$i]$left_arr[]=$str[$i];
else
$right_arr[]=$str[$i];
}
$left_arr=quicksort($left_arr);//进行递归;
$right_arr=quicksort($right_arr);
return array_merge($left_arr,array($key),$right_arr);//将左中右的值合并成一个数组;
}//以下是测试
$str=array(5,3,8,2,5,9,7,2,1,4,0);
print_r(quicksort($str));
?>


代码2:

复制代码 代码如下:


/* @快速排序法*/
function quickSort($left,$right,$arr){
$l = $left;
$r = $right;
$pivot = $arr[($left+$right)/2];
$temp = 0;

while($lwhile($arr[$l]$l++;
}
while($arr[$r]>$pivot){
$r--;
}

if($l>=$r) break;

$temp = $arr[$l];
$arr[$l] = $arr[$r];
$arr[$r] = $temp;

if($arr[$l]==$pivot) --$r;
if($arr[$r]==$pivot) ++$l;
}

if($l==$r){
$l++;
$r--;
}

if($leftquickSort($left, $r, $arr);
}elseif($right>$l){
quickSort($l, $right, $arr);
}else{
return $arr;
}
}

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.