This article mainly introduces php bubble sorting and quick sorting implementation methods, and analyzes in detail the recursion, traversal, and process control techniques of phpphp sorting algorithm in combination with examples, for more information about php bubble sorting and quick sorting algorithms, see the examples in this article. We will share this with you for your reference. The details are as follows:
$ A = array ('3', '8', '1', '4', '11', '7'); print_r ($ ); $ len = count ($ a); // small to large for ($ I = 1; $ I <$ len; $ I ++) {for ($ j = $ len-1; $ j >=$ I; $ j --) if ($ a [$ j] <$ a [$ j-1]) {// if it is from large to small, just change the judgment here to if ($ B [$ j]> $ B [$ j-1]) $ x = $ a [$ j]; $ a [$ j] = $ a [$ j-1]; $ a [$ j-1] = $ x ;}} print_r ($ a); j // Another method from small to large $ B = array ('4', '3', '8', '9 ', '2', '1'); $ len = count ($ B); for ($ k = 1; $ k <$ len; $ k ++) {for ($ j = $ len-1, $ I = 0; $ I <$ len-$ k; $ I ++, $ j --) if ($ B [$ j] <$ B [$ j-1]) {// if it's from big to small, just here If ($ B [$ j]> $ B [$ j-1]), you can get $ tmp = $ B [$ j]; $ B [$ j] = $ B [$ j-1]; $ B [$ j-1] = $ tmp;} print_r ($ B); echo "";} // function maopao ($ arr) {$ len = count ($ arr); for ($ I = 1; $ I <$ len; $ I ++) // do n-1-1 sort at most {$ flag = false; // The swap flag should be false for ($ j = $ len-1; $ j >=$ I; $ j --) {if ($ arr [$ j] <$ arr [$ j-1]) // exchange record {// if it is from large to small, just change the judgment here to if ($ arr [$ j]> $ arr [$ j-1]) $ x = $ arr [$ j]; $ arr [$ j] = $ arr [$ j-1]; $ arr [$ j-1] = $ x; $ flag = true; // An intersection occurs Change, so the switch flag is set to true} if (! $ Flag) // The return $ arr;} $ shuz = array ('2', '4', '1 ', '8', '5'); $ bb = maopao ($ shuz); print_r ($ bb); // Quick sort function kuaisu ($ arr) {$ len = count ($ arr); if ($ len <= 1) {return $ arr;} $ key = $ arr [0]; $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I <$ len; $ I ++) {if ($ arr [$ I] <= $ key) {$ left_arr [] = $ arr [$ I];} else {$ right_arr [] = $ arr [$ I] ;}}$ left_arr = kuaisu ($ left_arr ); $ right_arr = kuaisu ($ right_arr); return array_merge ($ left_arr, array ($ key), $ right_arr);} $ arr = array (, 34 ); print_r (kuaisu ($ arr ));
I hope this article will help you with php programming.