Examples of PHP two sort algorithms: php sort algorithm. Examples of PHP two sort algorithms: Although php does not emphasize the importance of sorting in web application development such as PHP, PHP has already brought () such as PHP two quick sorting algorithm instances, php sorting algorithm
Although in web application development such as PHP, we do not emphasize the importance of sorting too much, because PHP has such powerful sorting functions as sort, however, in some important cases, such as high concurrency, I think the influence of sorting algorithms cannot be ignored. So here we will introduce recursive sorting and iterative sorting.
Recursion:
/*** Quick sorting implemented by recursive method */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 ;}}
Iteration method:
/*** Fast sorting of iterative methods */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 ;}
Usage:
/*** Generate 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 ));
Although in web application development such as PHP, we do not emphasize the importance of sorting too much, because PHP already has such features as sort...