Php bubble sorting and quick sorting examples

Source: Internet
Author: User
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.

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.