Bubble sort-eight big sort three big find totals (3)

Source: Internet
Author: User

Basic ideas

22 The comparison between adjacent elements, if the former is greater than the latter, then the Exchange ;

Set the array length to n.

1. Compare the next two data, if the previous data is larger than the data, two data will be exchanged.

2. So that the No. 0 data of the array to N-1 data after one traversal, the largest one of the data is "sink" to the N-1 position of the array.

3. N=n-1, if N is not 0, repeat the previous two steps, otherwise the sort is complete.

Stability

Bubble sort is a stable sorting algorithm

Complexity of Time

If the initial state of the file is positive, a single scan will complete the sequencing. The required number of keyword comparisons and record moves has reached the minimum value:. So, the best time to bubble sort is complexity.

If the initial file is reversed, a sequencing is required. The Sub-keyword comparison (1≤i≤n-1) is performed for each order, and each comparison must move the record three times to reach the swap record location (Swap function). In this case, the comparison and the number of moves have reached the maximum value:

     Complexity of SpaceO (1)Usage ScenariosBubble sorting is an inefficient sort method, which can be used when the data size is very small. When the data size is large, it is best to use other sorting methods.Analysis and Code

Original Array to sort | 6 | 2 | 4 | 1 | 5 | 9 |

First trip sort (outer loop)

First 22 comparison 6 > 2 swap (inner loop)

Pre-swap Status | 6 | 2 | 4 | 1 | 5 |   9 | Post-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |

Second 22 comparison, 6 > 4 swap

Pre-swap Status | 2 | 6 | 4 | 1 | 5 |   9 | Post-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |

Third 22 comparison, 6 > 1 swap

Pre-swap Status | 2 | 4 | 6 | 1 | 5 |   9 | Post-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |

Fourth time 22 comparison, 6 > 5 swap

Pre-swap Status | 2 | 4 | 1 | 6 | 5 |   9 | Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Fifth time 22 comparison, 6 < 9 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 |   9 | Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Second trip sort (outer loop)

First 22 comparison 2 < 4 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 |   9 | Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Second 22 comparison, 4 > 1 swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 |    9 | Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Third 22 Comparisons, 4 < 5 non-exchangeable

Pre-swap Status | 2 | 1 | 4 | 5 | 6 |    9 | Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Fourth time 22 comparison, 5 < 6 no swap

Pre-swap Status | 2 | 1 | 4 | 5 | 6 |   9 | Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Third trip sort (outer loop)

First time 22 comparison 2 > 1 swap

Post-swap Status | 2 | 1 | 4 | 5 | 6 |   9 | post-swap status | 1 | 2 | 4 | 5 | 6 | 9 |

Second 22 comparison, 2 < 4 non-exchangeable

Post-swap Status | 1 | 2 | 4 | 5 | 6 |    9 | Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |

Third 22 Comparisons, 4 < 5 non-exchangeable

Post-swap Status | 1 | 2 | 4 | 5 | 6 |    9 | Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |

Four-trip sort (outer loop) No swap

Five-trip sort (outer loop) No swap

1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 5 voidPrintintA[],intNinti)6 {  7cout<<"Section"<<i+1<<"Tour:"; 8      for(intj=0; j<n; J + +)9     {  TenCOUT&LT;&LT;A[J] <<"  ";  One     }   Acout<<Endl;  - }     -  the voidBubblesort (intA[],intN) - {   -      for(inti =0; i< N-1; ++i) -     {   +          for(intj =0; J < N-1-i; ++J)//For (int j = n-1; j > i; j--) -         {   +             if(A[j] > a[j+1])   A             {   at                 intTMP =A[j]; -A[J] = a[j+1] ;  -a[j+1] =tmp;  -             }   -         }       - print (a,n,i); in     }   - }  to  + intMainintargcChar*argv[]) - { the     inta[6] = {6,2,4,1,5,9};  *   $Bubblesort (A,6); Panax Notoginseng     return 0; -}

You can see that since the third exchange has been ordered, after the comparison is redundant, so you can add a flag, if there is no exchange of elements in a loop, then the order is already sorted.

1 voidPrintintA[],intNinti)2 {  3cout<<"Section"<<i+1<<"Tour:"; 4      for(intj=0; j<n; J + +)5     {  6COUT&LT;&LT;A[J] <<"  "; 7     }  8cout<<Endl; 9 }    Ten  One voidBubblesort (intA[],intN) A {   -     BOOLIschanged =false; -      for(inti =0; i< N-1; ++i) the     {   -Ischanged =false; -          for(intj =0; J < N-1-i; ++J)//For (int j = n-1; j > i; j--) -         {   +             if(A[j] > a[j+1])   -             {   +                 intTMP =A[j]; AA[J] = a[j+1] ;  ata[j+1] =tmp;  -  -Ischanged =true; -             }   -         }       - print (a,n,i); in         if(ischanged = =false) -              Break;  to     }   + }  -  the intMainintargcChar*argv[]) * { $     inta[6] = {6,2,4,1,5,9}; Panax Notoginseng   -Bubblesort (A,6);  the     return 0; +}

Further optimization is done. If there are 100 number of arrays, only the first 10 unordered, and the next 90 are all ordered and are greater than the preceding 10 digits, then after the initial traversal, the position of the last interchange must be less than 10, and the position after the data must be ordered, record the position, The second time, just walk from the array head to this position.

1 //Bubble Sort 32 voidBUBBLESORT3 (intA[],intN)3 {4     intJ, K;5     intFlag;6     7Flag =N;8      while(Flag >0)9     {TenK =Flag; OneFlag =0; A          for(j =1; J < K; J + +) -             if(A[j-1] >A[j]) -             { theSwap (A[j-1], a[j]); -Flag =J; -             } -     } +}

Bubble sort-eight big sort three big find totals (3)

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.