: This article mainly introduces some simple php algorithm programs (bubble, fast, etc.). If you are interested in PHP tutorials, please refer to it.
Function buttle_sort ($ array) {$ len = count ($ array); if ($ len <2) {return $ array;} for ($ I = 0; $ I <$ len; $ I ++) {$ flag = false; // The swap flag should be false for ($ j = $ len-1; $ j> $ I; $ j --) {if ($ array [$ j] <$ array [$ j-1]) {$ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j-1]; $ array [$ j-1] = $ tmp; $ flag = true; // exchange occurred, therefore, set the switch flag to true }}if (! $ Flag) // The return $ array algorithm is terminated in advance because the current sorting has not been exchanged ;}
To be supplemented
The above introduces some simple php algorithm programs (bubble, fast, etc.), including some content, hope to be helpful to friends who are interested in PHP tutorials.