Sort algorithm (1)--bubble sort

Source: Internet
Author: User

Bubble sort

Bubble sort can be said to be the simplest sort of, of course, the complexity is also the highest

The implementation process for bubbling sorting: 22 is compared to each other, when the current is larger than the latter, the two are exchanged (assuming ascending order).

Then give a simple bubble sorting algorithm:

#define MAXSIZE 10
voidSwap (SqList *l,intIintj) { inttemp = l->R[i]; L->r[i] = l->R[j]; L-&GT;R[J] =temp;} typedefstruct { intR[maxsize +1]; intlength;} SqList;voidBubbleSort0 (SqList *m) { intLine , col; for(line =0; Line < L->length-1; line++) { for(col = line +1; Col < l->length; col++) { if(L->a[line] > L->a[col]) {swap (L, line, col) }}}

This is the simplest sort of algorithm that we can write out of, not authentic bubble sort.

Here's an authentic bubble sort:

void BubbleSort1 (SqList *L) {    int line , low;      for 0 1; line++)    {        for1, low >= line; low--)        {            if  1]            {                    swap (L, line, low);     }}}}

Obviously, this sort algorithm is much better than the previous one, and the data behind it is gradually floating up (before), and the data that is slightly smaller (more than the first one that floats up) is floated to the front by this sort. And the last one is a comparison of each one behind each one.

Can bubble sorting still improve?

The answer is yes.

Imagine what it would be like if there was no data exchange or little data exchange.

Let's say {2, 1, 3, 4, 5, 6, 7, 8, 9}, sort it, obviously, except for the 1 and 2 exchanges, there's no other data to exchange, but if we follow the above algorithm, we're going to do a for loop and compare the 22 to the other, even without the data exchange (the computer doesn't know that The number of groups in the end there is no order). Then, we can think of an improvement method, if there is no data exchange, do not compare.

The specific improvement code is as follows:

voidBubbleSort2 (SqList *m) {    intLine , col; BOOLFlag =True;  for(line =0; Line < L->length-1&& Flag; line++) {flag=False;  for(col = l->length-1; Col >= Line; low--)        {            if(A[col] > A[col +1]) {swap (L, col, Col+1); Flag= True;//If there is data exchange, then flag is true, if not, it is false .            }        }    }}

Above.

Write this a little bit first.

Because the roommate too noisy can not sleep, so just write ...

It's so sad.

Almost forgot the complexity of the analysis:

The complexity of this algorithm is O (n^2), the specific analysis is as follows:

Worst-case situation, is the original sort and we want to sort the exact opposite, for example, we want {1, 2, 3, 4, 5}, and the original sequence is {5, 4, 3, 2, 1}, then each step of the comparison will need to change the position, the first need to compare and move 4 times, the second to compare and move 3 times, The third one needs to compare and move two times, the fourth need to compare and move 1 times, a total of 10 times, that is (n-1) + (n-2) + (n-3) + ... + 1 = n (n-1)/2.

And the best case, of course, is not to move at once, this time the algorithm is the complexity of O (n).

Sort algorithm (1)--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.