基于PHP的快速排序
发布时间:2024-11-25
发布时间:2024-11-25
快速排序基于PHP
<?php
header("Content-type:text/html;charset=utf-8");
for($i=0;$i<1000;$i++){
$clean[$i]=rand(0,1000);
}
//快速排序
function quickSort($arr)
{
$len = count($arr);
if($len <= 1) {
return $arr;
}
$key = $arr[0];
$left_arr = array();
$right_arr = array();
for($i=1; $i<$len; $i++){
if($arr[$i] <= $key){
$left_arr[] = $arr[$i];
} else {
$right_arr[] = $arr[$i];
}
}
$left_arr = quickSort($left_arr);
$right_arr = quickSort($right_arr);
return array_merge($left_arr, array($key), $right_arr); }
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
快速排序基于PHP
$this->StartTime = $this->get_microtime();
}
function stop()
{
$this->StopTime = $this->get_microtime();
}
function spent()
{
return round(($this->StopTime - $this->StartTime) * 1000, 1);
}
}
$runtime= new runtime;
$runtime->start();
$clean=quickSort($clean,0,999);
$runtime->stop();
echo "页面执行时间: ".$runtime->spent()." 毫秒";
print_r($clean);
?>