: This article mainly introduces php bubble sorting for quick sorting. For more information about PHP tutorials, see. /******
1) bubble sorting: exchange values in pairs. the smallest value is the leftmost value, just like the lightest bubble at the top.
2) exchange the number of the entire column every two times. The smallest number is on the leftmost side. each time we get the smallest number in the remaining number, the "popped" array forms an ordered interval, the remaining values constitute an unordered interval, and each element value in the ordered interval is smaller than that in the unordered interval.
3) fast sorting: the number of baselines, two arrays left and right, recursively called, and merged.
4)
Insert sort: The sorting interval is divided into two parts: ordered on the left and unordered on the right. The first element from the right interval is inserted into the left interval. if this element is larger than the element on the far right of the left interval, it is left in the original position, if this element is smaller than the element on the rightmost side of the left edge range, it is inserted in the original position of the rightmost 5) element. at the same time, the rightmost element shifts one bit to the right, and the calculator minus one, repeat the preceding steps to repeat the previous steps until the previous element is smaller than the element to be inserted.
6) pay attention to the processing of the range endpoint value and the subscript of the first element of the array is 0.
***/
$ 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); // 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 judge here Change it to if ($ B [$ j]> $ B [$ j-1]) instead of $ 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; // if an exchange occurs Change Flag 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); returnarray_merge ($ left_arr, array ($ key), $ right_arr);} $ arr = array (, 34 ); print_r (kuaisu ($ arr ));
The above describes the php bubble sort quick sorting, including the insert sort content, and hope to be helpful to friends who are interested in the PHP Tutorial.