Bubble sorting principle

Source: Internet
Author: User
I have seen a lot of Introduction to Bubble sorting, including teaching books. I personally feel that many of the descriptions are not very clear, or my personal understanding may be poor, however, the figure found on the Internet looks very clear. I hope it can help some friends. The Bubble sorting principle is to compare the adjacent numbers in the order of ascending to ascending or ascending to smallest, the largest or smallest number is switched to the last digit, and then compared and exchanged from the beginning until the last digit ends. The rest are similar examples.

You can also describe the bubble sort as follows:

Is to compare the keywords of the first record with those of the second record,

If the latter is smaller than the former, the second and third are compared, and so on.

Than the end of a trip, the largest one has been placed in the last position, so that you can compare the number of the previous N-1 recycling.

Stable, time complexity O (N ^ 2)

 

Instance: // Bubble sort (array sorting)

Function bubble_sort ($ array){
$ COUNT = count ($ array );
If ($ count <= 0) return false;
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ J = $ count-1; $ j> $ I; $ J -){
If ($ array [$ J] <$ array [$ J-1]) {
$ TMP = $ array [$ J];
$ Array [$ J] = $ array [$ J-1];
$ Array [$ J-1] = $ TMP;
}
}
}
Return $ array;
}

Bubble sorting principle

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.