PHP Two quick sort algorithm examples, PHP sorting algorithm
Although in Web application development such as PHP, we do not emphasize the importance of sorting too much, because PHP itself has taken such a powerful sort function as sort (), but in some important occasions, such as some high concurrency, I think the effect of sorting algorithm can not be ignored. So here we introduce recursive sorting and iterative sorting.
Recursive method :
/*** recursive method for fast sequencing */function quicksort ($seq) { $k = $seq [0]; $x = Array (); $y = Array (); for ($i =1; $i < $_size; $i + +) { if ($seq [$i] <= $k) { $x [] = $seq [$i]; } else { $y [] = $seq [$i]; } } $x = Quicksort ($x); $y = Quicksort ($y); Return Array_merge ($x, Array ($k), $y); } else { return $seq; }}
Iterative method:
Quick sort of/*** iterative method */function Quicksortx (& $seq) { $stack = array ($seq); $sort = Array (); while ($stack) { $arr = Array_pop ($stack); if (count ($arr) <= 1) { if (count ($arr) = = 1) { $sort [] = & $arr [0]; } Continue; } $k = $arr [0]; $x = Array (); $y = Array (); $_size = count ($arr); for ($i =1; $i < $_size; $i + +) { if ($arr [$i] <= $k) { $x [] = & $arr [$i]; } else { $y [] = & $arr [$i]; } ! Empty ($y) && Array_push ($stack, $y); Array_push ($stack, Array ($arr [0]); ! Empty ($x) && Array_push ($stack, $x); } return $sort;}
Use:
/*** produces a random array */for ($i =0; $i <5; $i + +) { $testArr []=mt_rand (0,100);} Var_dump ($TESTARR); Var_dump (Quicksort ($TESTARR)); Var_dump (Quicksortx ($TESTARR));
http://www.bkjia.com/PHPjc/958719.html www.bkjia.com true http://www.bkjia.com/PHPjc/958719.html techarticle php two fast sorting algorithm examples, PHP sorting algorithm although in the development of Web applications such as PHP, we are not too stressed the importance of sorting, because PHP itself has taken such as sort () and so on ...