Let algorithms talk about Bubble Sorting

Source: Internet
Author: User

 

I. Bubble Sorting Algorithm and Optimization

1. Common Code

 

/*************************************** * ************************ All Rights Reserved (C) 2014, company name. ** File name: Bubble sorting method * Content Abstract: none * Other Instructions: none * Current version: V1.0 * Author: If the cloud flow Wind * completion date: 2014.5.15 *************************************** * ***********************/# include
 
  
# Define N 7 void disp (void); int a [N] = {5, 0, 7, 1, 9, 11, 12};/* sorting function */void BubbleSort (void) {int I, j, temp; for (I = 0; I <N-1; I ++) // Compare N rounds, compare the largest number in each round (shift the large number to the right) {printf (New Round:); for (j = 1; j <N-I; j ++) // The number of comparisons in each round {if (a [j-1]> a [j]) // if the number of left faces is greater than the number of right faces, the switching position (shifted right) {temp = a [j]; a [j] = a [J-1]; a [J-1] = temp;} disp (); // print whether or not to exchange, convenient optimization}/* output function */void disp (void) {int I; printf (sorting result:); for (I = 0; I
  
   

 

2. Optimization 1

 

/*************************************** * ************************ All Rights Reserved (C) 2014, company name. ** File name: Bubble sorting method * Content Abstract: none * Other Instructions: none * Current version: V1.1 * OPERATOR: If the cloud flow Wind * completion date: 2014.5.16 *************************************** * ***********************/# include
    
     
# Define N 7 void disp (void); int a [N] = {5, 0, 7, 1, 9, 11, 12};/* sorting function */void BubbleSort2 () {int j, k, temp; bool flag; k = N; flag = true; while (flag) // use the flag to determine whether to perform the next round {flag = false; printf (New Round:); for (j = 1; j <k; j ++) {if (a [j-1]> a [j]) // introduce the flag in the comparison. If there is a comparison in this round, the next {temp = a [j]; a [j] = a [J-1]; a [J-1] = temp; flag = true;} disp ();} k --;}/* output function */void disp (void) {int I; printf (sorting result:); for (I = 0; I
     
      

 

3. Optimization 2 (final)

 

/*************************************** * ************************ All Rights Reserved (C) 2014, company name. ** File name: Bubble sorting method * Content Abstract: none * Other Instructions: none * Current version: V1.2 * OPERATOR: If the cloud flow Wind * completion date: 2014.5.16 *************************************** * ***********************/# include
       
        
# Define N 7 void disp (void); int a [N] = {5, 0, 7, 1, 9, 11, 12 }; /* sorting function * // Bubble Sorting 3 void BubbleSort3 (void) {int j, k, temp; int flag; flag = N; while (flag> 0) // introduce flag position {k = flag; flag = 0; // clear the flag position printf (New Round:); for (j = 1; j <k; j ++) {if (a [j-1]> a [j]) // record has a comparison flag, optimization algorithm, reduce empty loop {temp = a [j]; a [j] = a [J-1]; a [J-1] = temp; flag = j;} disp () ;}}/ * output function */void disp (void) {int I; printf (sorting result:); for (I = 0; I
        
         

 

2. algorithm talking

Int a [N] = {5, 0, 7, 1, 9, 11, 12 };

1. Common Programs

This is our most commonly used algorithm, array if N number, to carry out the N-1 wheel, the I round compared N-I times, if the value of the left side is greater than the right face value, for location interchange.

2. Optimization 1

We add a flag. If there is no change in the previous rotation, we will exit directly, avoiding the empty loop after 3rd rounds.

3. Optimization 2 (final)

Even after optimization, we can still see the empty loop of the second and third rounds, especially the third. This time we not only add a flag, but also let the flag record the position when we change the value. Next time we recycle it, we only need to loop through the previous number.

For example, in the first loop of the array, when the last flag value is a [2]> a [3] (7> 1), the flag is copied to 3, after that, the values are not assigned again (that is, the subsequent order is correct). So next time, you only need to rank the first three values. So there are only two results in the next round (eliminating unnecessary null loops ).

 

 

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.