This article introduces the recursive algorithm implementation and iterative algorithm implementation of the fast sorting problem in PHP, has a certain reference value, now share to everyone, the need for friends can refer to
Implementation code
Code Address: Https://github.com/ParrySMS/Exp/tree/master/ProLang/quickSort
Recursive methodquickSortRec.php
<?php/** * Created by Phpstorm. * User:l * date:2018-4-13 * time:23:27 *//** recursive Quick sort * @param array $ar * @return array */function quicksortr (array $ar) { //judgment array length $size = sizeof ($ar); if ($size <=1) { return $ar; } The data $left = Array () with two arrays accepting smaller than the cursor key and larger than key; $right = Array (); $key = $ar [0]; for ($i =1; $i < $size; $i + +) { if ($ar [$i]<= $key) { $left [] = $ar [$i]; } else{ $right [] = $ar [$i]; } } The internal reordering $left = QUICKSORTR ($left); $right = QUICKSORTR ($right); Finally merge return Array_merge ($left, Array ($key), $right);}
Iterative method quicksortiter.php
<?php/** * Created by Phpstorm. * User:l * date:2018-4-14 * time:14:51 *//** Iteration method * @param array $ar * @return array */function quicksorti (array $ar) { $stack = Array ($ar); $sort = Array (); Determine the length of the array $size = sizeof ($ar); if ($size <= 1) {return $ar; }//stack empty that jumps out of the loop while ($stack) {$arr = Array_pop ($stack); if (count ($arr) <= 1) {if (count ($arr) = = 1) {$sort [] = & $arr [0]; } continue; } $key = $arr [0]; $high = Array (); $low = Array (); The Data $_size = COUNT ($arr) is smaller than the cursor key and larger than key with two arrays respectively; for ($i = 1; $i < $_size; $i + +) {if ($arr [$i] <= $key) {$high [] = & $arr [$i]; } else {$low [] = & $arr [$i]; }} if (!empty ($low)) {//Data inbound Array_push ($stack, $low); } array_push ($stack, Array ($arr [0])); if (!empty ($high)) {ARray_push ($stack, $high); }} return $sort;}
Execution Time Test Scripttest.php
<?php/** * Created by Phpstorm. * User:l * DATE:2018-4-17 * time:23:45 */require "quicksortiter.php" Require "quicksortrec.php";d efine (' SORT_TIMES ', 10 0);d efine (' SIZE ', +), function rowtable () {unset ($row); $row = Array (); for ($i = 0; $i < sort_times; $i + +) {$row = Getsortrow ($row); } foreach ($row as $r) {print <<< TR <tr> <td> $r->iter</td> <td> ; $r->rec</td> </tr>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 = * ($etime-$stime);//echo "<br/>"; $stime = Microtime (true); $iterAr = Quicksorti ($ar); $etime = Microtime (true); $iterTime = ($etime-$stime);//Print_r ($RECAR);//echo "<br/>";//Print_r ($ITERAR); $row [] = (object) ["ITER" -$iterTime, "rec" +-$recTime]; return $row;}? ><table border= "1" > <tr> <th> iterative iter/ms</th> <th> recursive rec/ms</th> </tr> <?php rowtable ();?></table>
5,000 execution time Efficiency comparison
mode/Execution ms time |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
2.840572476 |
0.03862993 |
Recursive Rec/ms |
3.071363568 |
0.06567554 |
Mode |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
0.987666035 |
0.015847294 |
Recursive Rec/ms |
0.987947607 |
0.036398175 |
Mode |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
0.081454897 |
0.000522679 |
Recursive Rec/ms |
0.066546392 |
0.000362922 |
Implementation code
Code Address: Https://github.com/ParrySMS/Exp/tree/master/ProLang/quickSort
Recursive methodquickSortRec.php
<?php/** * Created by Phpstorm. * User:l * date:2018-4-13 * time:23:27 *//** recursive Quick sort * @param array $ar * @return array */function quicksortr (array $ar) { //judgment array length $size = sizeof ($ar); if ($size <=1) { return $ar; } The data $left = Array () with two arrays accepting smaller than the cursor key and larger than key; $right = Array (); $key = $ar [0]; for ($i =1; $i < $size; $i + +) { if ($ar [$i]<= $key) { $left [] = $ar [$i]; } else{ $right [] = $ar [$i]; } } The internal reordering $left = QUICKSORTR ($left); $right = QUICKSORTR ($right); Finally merge return Array_merge ($left, Array ($key), $right);}
Iterative methodquickSortIter.php
<?php/** * Created by Phpstorm. * User:l * date:2018-4-14 * time:14:51 *//** Iteration method * @param array $ar * @return array */function quicksorti (array $ar) { $stack = Array ($ar); $sort = Array (); Determine the length of the array $size = sizeof ($ar); if ($size <= 1) {return $ar; }//stack empty that jumps out of the loop while ($stack) {$arr = Array_pop ($stack); if (count ($arr) <= 1) {if (count ($arr) = = 1) {$sort [] = & $arr [0]; } continue; } $key = $arr [0]; $high = Array (); $low = Array (); The Data $_size = COUNT ($arr) is smaller than the cursor key and larger than key with two arrays respectively; for ($i = 1; $i < $_size; $i + +) {if ($arr [$i] <= $key) {$high [] = & $arr [$i]; } else {$low [] = & $arr [$i]; }} if (!empty ($low)) {//Data inbound Array_push ($stack, $low); } array_push ($stack, Array ($arr [0])); if (!empty ($high)) {ARray_push ($stack, $high); }} return $sort;}
Execution Time Test Scripttest.php
<?php/** * Created by Phpstorm. * User:l * DATE:2018-4-17 * time:23:45 */require "quicksortiter.php" Require "quicksortrec.php";d efine (' SORT_TIMES ', 10 0);d efine (' SIZE ', +), function rowtable () {unset ($row); $row = Array (); for ($i = 0; $i < sort_times; $i + +) {$row = Getsortrow ($row); } foreach ($row as $r) {print <<< TR <tr> <td> $r->iter</td> <td> ; $r->rec</td> </tr>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 = * ($etime-$stime);//echo "<br/>"; $stime = Microtime (true); $iterAr = Quicksorti ($ar); $etime = Microtime (true); $iterTime = ($etime-$stime);//Print_r ($RECAR);//echo "<br/>";//Print_r ($ITERAR); $row [] = (object) ["ITER" -$iterTime, "rec" +-$recTime]; return $row;}? ><table border= "1" > <tr> <th> iterative iter/ms</th> <th> recursive rec/ms</th> </tr> <?php rowtable ();?></table>
5,000 execution time Efficiency comparison
mode/Execution ms time |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
2.840572476 |
0.03862993 |
Recursive Rec/ms |
3.071363568 |
0.06567554 |
Mode |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
0.987666035 |
0.015847294 |
Recursive Rec/ms |
0.987947607 |
0.036398175 |
Mode |
average (array length) |
Variance (array length) |
Iterative Iter/ms |
0.081454897 |
0.000522679 |
Recursive Rec/ms |
0.066546392 |
0.000362922
|
Related recommendations:
A sort of bubble sort in PHP
PHP sorting algorithm series of Insert Sort instance sharing
PHP Sorting algorithm heap sorting detailed