The second of the algorithm design of bubble sort

Source: Internet
Author: User


Bubble Sort Process

1. First compare the adjacent two elements, if the previous data is greater than the subsequent data, then the two numbers are exchanged, then push until the completion of the N-1 record and the nth record Exchange (first bubbling).

2. Then proceed to the second air bubble. By the first bubble, the end is the maximum number, so the second pass comparison before the number of N-1

3. Third trip ... Compare the number of N-2

...................................................................................


Bubble Analysis

Through the above process analysis, you can know that bubbling is like an inverted triangle.

First time bubble, compare n times

Second bubble, compare N-1 times

.....................

As shown in the triangle


Code Analysis

From the bubbling process analyzed above, we have a much simpler implementation of the code.

1. There is a process of two number exchange before and after.

2. There is a process of decreasing the number of exchanges



Code Implementation

<span Style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//<summary>//bubble sequencing implementation process//</summary>//<param name= "a" > Array A is used to hold the numbers to be sorted and            lt;/param>//<param name= "n" > number of array elements </param> void Bubblesort (int[] A, int n) {                    int i,j,c; The outer loop, which is used to assist, decrements the number of times for (i=0;i<n;i++) {//inner loop used to do two-digit exchange for (j=1;j&                                      lt;n-i;j++) {//two-digit exchange process if (A[j-1]>a[j])                        {//Introduce third party, do auxiliary c = a[j-1];                        A[J-1] = A[j];                    A[J] = c; }}}}</span> 


Code analysis, the above code can also be optimized, because if there is no exchange in the process of a cycle, then we can know that the order has been completed, you do not have to proceed to compare the judgment, so we can set a flag bit, to judge.


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > static void Bubblesort (int[] A, int n)        {            int i,j,c;            BOOL Flag;          The outer loop, which is used to assist, decrements the number of times for            (i=0;i<n;i++)            {                flag = false;//To set a flag bit, the default value is False                //The inner loop is used to exchange two numbers before and after For                (j=1;j<n-i;j++)                {                  //two number exchange process                    if (A[j-1]>a[j])                                      {                        //introduce a third party, do auxiliary                        C = a[j-1];                        A[J-1] = a[j];                        A[J] = C;                        Flag = true;  If an exchange occurs, the flag bit is true                    }                //Here to determine the flag bit, if false, prove that there is no exchange, so it is sorted, you can directly stop the process                if (!flag) break ;                             }        } </span>

If you understand the nature of the bubble sort, the code is also good to achieve more, to remember the above inverted triangle, understand two points.

1. The number of comparisons is less

2. Compare before and after



So the code above can also be written in the while statement, which is actually the same

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//Optimized bubbling algorithm for        static void BubbleSort2 (int []a,int N)        {            int J;            BOOL Flag;            int C;            Flag-            =true;            while (flag)            {                flag =false;                for (j=1;j<n;j++)                {                    if (A[j-1]>a[j])                    {                        c = a[j-1];                        A[J-1] = a[j];                        A[J] = C;                        Flag = true;                    }                }                               The number of times is decreasing                n--;}        } </span>


Learning Experience

Online This sort of code a lot of, as long as you can understand the essence, the application will not bother. But the premise of understanding the essence is to do a lot of writing and painting operations. Other people's things, is always someone else's, so we have to through research to become ourselves, can do for me.

The second of the algorithm design of bubble sort

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.