Bubble sort Bubble Sort

Source: Internet
Author: User

Bubble sort Basic idea: 22 compare the keywords of adjacent records, if the reverse order is exchanged until there is no reverse order.

1 #defineMAXSIZE 102 3typedefstruct 4 {5     intR[maxsize];//r[0] used as a sentinel or temporary variable6     intLength//length of the Record order table7 }sqlist;8 9 voidSwap (SqList *l,intIintj)Ten { One     inttemp = l->R[i]; AL->r[i] = l->R[j]; -L->R[J] =temp; -}

1. Beginner version

The idea is to make each keyword compare to every keyword behind it, and if it's big, swap it. Strictly speaking, it is not a standard bubble sorting algorithm, it does not meet the "22 compare adjacent records" bubble sort idea.

1 voidBubblesort (SqList *L)2 {3     intI, J;4      for(I=1; i<l->length; i++)//Notice the iteration range5     {6          for(j=i+1; j<=l->length; J + +)//Notice the iteration range7         {8             if(L->r[i] > l->R[j])9             {Ten swap (L, I, j); One             } A         } -     } -}

2. Authentic Bubbling Algorithm

By comparing the next 22 neighbors, the smaller number floats as slowly as a bubble, so it is a bubbling algorithm.

1 voidBubblesort (SqList *L)2 {3     intI, J;4      for(i=1; i<l->length; i++)5     {6          for(j=l->length-1; j>=i; j--)//J is the loop from the back forward7         {8              if(L->r[j] > l->[j+1])//Condition: The former is greater than the latter9              {TenSwap (L, J, j+1); One              } A         } -     }  -  the}

But its disadvantage is that when the number in the next part is ordered, such as {2,1,3,4,5,6,7,8,9}

When the first round is compared (the outer loop, i.e. i = 1), swapped 2 and 1, the sequence is ordered, but the algorithm will execute the i = 2 to 9 and J in each loop once, although no data is exchanged, but a large number of comparisons are much more redundant.

3. Improved bubbling algorithm

The key to the code churn is the I variable's for loop, which adds a judgment on whether flag is true, and with this improvement, the bubbling sort has some performance improvements that can avoid meaningless cyclic judgments in an ordered situation.

1 voidBubblesort (SqList *L)2 {3     intI, J;4Status flag =TRUE;5      for(i=1; I<l->length && Flag; i++)//exit the loop if flag is false6     {7flag = FALSE;//Initialize to Flase8 9          for(j=l->length-1; j>=i; j--)Ten         { One              if(L-r[j] > l->r[j+1]) A              { -Swap (L, J, j+1); -flag = TRUE;//If there is data exchange, flag is true the              } -         } -     }   -}

4. Bubble sequencing Complexity Analysis

Best case: The table itself is ordered, according to the improved code, for n-1 times, no data exchange, so the time complexity is O (n).

Worst case scenario: The table is in reverse order and needs to compare ∑1n-1 (n-i) = n-1 + n-2 + ... + 1 = n (n-1)/2 times, and record movement for the order of magnitude.

So the total time complexity is O (N2).

Bubble sort 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.