Php bubble sorting

Source: Internet
Author: User
Php bubble sorting

After such a long time of contact with php, three sorting methods were used: Bubble sorting, fast sorting, and barrel sorting. today we will learn about bubble sorting:

So what is bubble sorting? just like bubbles in the river, one bubble emerges, and here is a number. its principle is repeated visit (traversal) the number of columns to be sorted. compare two adjacent numbers, move the large number to the right, and Traverse until all the numbers are sorted in ascending order. Compare the current maximum number each time. compare the remaining number in the next round and use two cycles to control the number of rounds in the outer loop and the elements in the comparison in the inner loop control:

Code on

  1. /**
  2. * Bubble sorting
  3. */
  4. $ List = Array (6,8, 7,2, 3,4, 1 );
  5. Echo "before sorting ";
  6. Print_r ($ list );
  7. Function mao ($ arr ){
  8. For ($ I = 1, $ len = count ($ arr); $ I <$ len; ++ $ I) {// Number of outer loop arrays-1
  9. For ($ k = 0, $ klen = $ len-$ I; $ k <$ klen; ++ $ k) {// an inner loop that compares two array elements
  10. If ($ arr [$ k]> $ arr [$ k + 1]) {
  11. $ Temp = $ arr [$ k];
  12. $ Arr [$ k] = $ arr [$ k + 1];
  13. $ Arr [$ k + 1] = $ temp;
  14. }
  15. }
  16. }
  17. Return $ arr;
  18. }
  19. Echo"
    ";
  20. Print_r (mao ($ list ));

In the process of bubbling, the idea has been in the minds of others. in the process of Baidu, I saw another method and wrote it as well:

  1. $ List = Array (6,8, 7,2, 3,4, 1 );
  2. Echo "before sorting ";
  3. Print_r ($ list );
  4. Function mao ($ arr ){
  5. For ($ I = 0, $ len = count ($ arr)-1; $ I <$ len; ++ $ I) {// traverse the first layer of an outer loop
  6. // The inner layer loop. add one on the outer layer to control the comparison of the two elements.
  7. For ($ k = $ I + 1; $ k <= $ len; ++ $ k ){
  8. If ($ arr [$ I]> $ arr [$ k]) {
  9. $ Temp = $ arr [$ I];
  10. $ Arr [$ I] = $ arr [$ k];
  11. $ Arr [$ k] = $ temp;
  12. }
  13. }
  14. }
  15. Return $ arr;
  16. }
  17. Echo"
    ";
  18. Print_r (mao ($ list ));

  

In the process of writing, I admire the latter method. His thinking is very flexible, because the first method is based on our normal thinking. it is very straightforward, I felt very interesting in thinking,

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.