这篇文章介绍的内容是关于在PHP快速排序问题的递归算法实现和迭代算法实现 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
实现代码
代码地址:https://github.com/parrysms/exp/tree/master/prolang/quicksort
递归法 quickSortRec.php
迭代法 quickSortIter.php
执行时间测试脚本 test.php
| $r->iter |
$r->rec |
TR;
}
}function getSortRow(array $row){
unset($ar); $ar = array(); for ($i = 0; $i < SIZE; $i++) { $ar[] = rand(0, SIZE*2);
} $stime = microtime(true); $recAr = quickSortR($ar); $etime = microtime(true); $recTime = 1000 * ($etime - $stime);// echo"
";
$stime = microtime(true); $iterAr = quickSortI($ar); $etime = microtime(true); $iterTime = 1000 * ($etime - $stime);// print_r($recAr);// echo "
";// print_r($iterAr);
$row[] = (object)["iter" => $iterTime, "rec" => $recTime]; return $row;
}?>
5000次执行时间效率对比
| 模式/执行ms时间 |
平均数(数组长度1000) |
方差(数组长度1000) |
| 迭代 Iter /ms |
2.840572476 |
0.03862993 |
| 递归 Rec /ms |
3.071363568 |
0.06567554 |
| 模式 |
平均数(数组长度400) |
方差(数组长度400) |
| 迭代 Iter /ms |
0.987666035 |
0.015847294 |
| 递归 Rec /ms |
0.987947607 |
0.036398175 |
| 模式 |
平均数(数组长度50) |
方差(数组长度50) |
| 迭代 Iter /ms |
0.081454897 |
0.000522679 |
| 递归 Rec /ms |
0.066546392 |
0.000362922 |
实现代码
代码地址:https://github.com/parrysms/exp/tree/master/prolang/quicksort
递归法 quickSortRec.php
迭代法 quickSortIter.php
执行时间测试脚本 test.php
| $r->iter |
$r->rec |
TR;
}
}function getSortRow(array $row){
unset($ar); $ar = array(); for ($i = 0; $i < SIZE; $i++) { $ar[] = rand(0, SIZE*2);
} $stime = microtime(true); $recAr = quickSortR($ar); $etime = microtime(true); $recTime = 1000 * ($etime - $stime);// echo"
";
$stime = microtime(true); $iterAr = quickSortI($ar); $etime = microtime(true); $iterTime = 1000 * ($etime - $stime);// print_r($recAr);// echo "
";// print_r($iterAr);
$row[] = (object)["iter" => $iterTime, "rec" => $recTime]; return $row;
}?>
5000次执行时间效率对比
| 模式/执行ms时间 |
平均数(数组长度1000) |
方差(数组长度1000) |
| 迭代 Iter /ms |
2.840572476 |
0.03862993 |
| 递归 Rec /ms |
3.071363568 |
0.06567554 |
| 模式 |
平均数(数组长度400) |
方差(数组长度400) |
| 迭代 Iter /ms |
0.987666035 |
0.015847294 |
| 递归 Rec /ms |
0.987947607 |
0.036398175 |
| 模式 |
平均数(数组长度50) |
方差(数组长度50) |
| 迭代 Iter /ms |
0.081454897 |
0.000522679 |
| 递归 Rec /ms |
0.066546392 |
0.000362922
|
相关推荐:
PHP排序之冒泡排序
PHP排序算法系列之插入排序实例分享
立即学习“PHP免费学习笔记(深入)”;
PHP排序算法之堆排序详解