I'll talk about it. Bubble sort

Source: Internet
Author: User

has an array:

int array [] = {5,4,3,2,1};

Now we need to sort from small to large, so we can imagine that after the sort is {1,2,3,4,5}

So the bubble sort principle is:

For each round, the largest is placed on the last side, which is also the origin of the bubbling name.

Then let's do the first round of comparisons!

 for (int i = 0; i < a.length-1; i++) {    if(a[i] > [A + I]) {//if a[i] is larger than a[i+1], both In the Exchange        order int temp = a[i + 1]        ; + 1] = a[i];         = temp;    }   }    

To subdivide the process of this comparison:

i = 0, a[0] and a[1], the order of the finished array should be:

4,5,3,2,1

i = 1, a[1] and a[2], when compared, the order of the arrays should be:

4,3,5,2,1

i = 2, a[2] and a[3], when compared, the order of the arrays should be:

4,3,2,5,1

i = 3, a[3] and a[4], when compared, the order of the arrays should be:

4,3,2,1,5

When i=4, because to Judge I<a.length-1, not satisfied, so quit the comparison.

So after the first round of comparison, the order of the arrays should be {4,3,2,1,5}.

At this point, the comparison results, but also need to compare, then in the end need to compare several rounds? How many rounds? What about the wheel? How about it ~ ~ ~??

If "Every time the biggest number to the last" to calculate a round, then in the most extreme cases [such as this example], then should be the n-1 round, then another classmate asked, why is the n-1 round it?

Then for example, 2 numbers {2,1} compare, how many rounds will it take to put the big one back? 1 rounds, that is, 2-1 rounds, hehe, promotion is the n-1 round.

So the bubbling code should look something like this:

1  for(intj = 0; J < A.length-1; J + +) {//A.length-1 marked n-1 wheel2      for(inti = 0; i < a.length-1; i++){3      if(a[i] > [A + i]) {//if A[i] is larger than a[i+1, the two exchange order4          inttemp = a[i + 1];5A[i + 1] =a[i];6a[I] =temp;7      }   8   } 9}

Bubbling up here is actually over.

But in fact there is an optimization space ...

We have found in front

After the first round of sorting, the largest one has reached the last position.

After the second round of sorting, the second largest one has been to the penultimate position.

And so on, so every round of sequencing, then the "corresponding to the reciprocal position" of the number is actually no comparison ...

For example, after the first round of sorting, then in the second round of sorting, the countdown to the first actually did not have to compare, because again the first round of sorting, has been very clear, the last one is the largest.

There is a place for optimization here:

 for (int//  a.length-1 marked n-1 wheel      for (int i = 0; i < a.length-1-J; i++) {// subtract a "j" here, so the number of "corresponding reciprocal positions" is not compared with each other.      if // if A[i] is larger than a[i+1, the two exchange order          int temp = a[i + 1];           + 1] = a[i];           = temp;}}  }

Bubble should be more complete here, then your own code to write their own must be tested.

So how to test it?

Let me write a few use cases, write the array elements directly:

A [] = {};

A [] = { -1,-2,0,2,1}

A [] = {10,9,8,7,6,5,4,3,2,1}

A [] = {1,2,3,4,5,6,7,8,9,10}

The interview is often asked. And because of this I missed a few good opportunities. Hope in the future will not be brushed because of this, hehe, 2015, good luck often come ~

I'll talk about it. 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.