Php bubble sorting quick sorting, php bubble sorting _ PHP Tutorial

Source: Internet
Author: User
Php bubble sorting quick sorting, php bubble sorting. Php bubble sorting: php bubble sorting ******* 1) bubble sorting: exchange values in pairs, and the smallest value is the leftmost value, just like the lightest bubble at the top. 2) exchange php bubble sorting for the entire column to sort the data quickly, and php bubble sorting

/******
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 sorting: 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, if this element is smaller than the rightmost element in the left edge range, it is inserted in the rightmost 5) original position of the element. at the same time, the rightmost element shifts one 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 here Change it to if ($ B [$ j]> $ B [$ j-1]) and then $ 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; // exchange occurred, 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 ));

Http://www.bkjia.com/PHPjc/1082224.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1082224.htmlTechArticlephp bubble sort quick sorting, php bubble sort/****** 1) bubble sort: two exchange values, the smallest value on the leftmost, such as the lightest bubble on the top. 2) exchange the number of the entire column in pairs...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.