C ++ data structures and algorithms: Bubble sorting and Improved Algorithms

Source: Internet
Author: User
Tags cmath

Bubble Sorting is a simple sort. In this sort, the "Bubble Policy" is used to move the maximum element to the rightmost. During the bubble process, two adjacent elements are compared. If the left side is greater than the right side, two elements are exchanged. After such a bubble, you can ensure that the maximum is on the rightmost side. Then, execute n bubbles and then sort them.

ProgramCodeAs follows:

 //  Bubblesort. cpp: defines the entry point of the console application.  //  # Include  "  Stdafx. h  "  # Include <Cmath> # Include <Iostream> Using   Namespace  STD; # Define Maxnum 20 Template <Typename T> Void Swap (T & A, T & B ){  Int T = A; = B; B = T;} Template <Typename T> Void Bubble (t a [], Int  N ){  //  Move the largest element in array a [0: n-1] To the right through bubbling     For ( Int I = 0 ; I <n- 1 ; I ++ ){  If (A [I]> A [I + 1  ]) Swap (A [I], a [I + 1  ]) ;}} Template <Typename T> Void Bubblesort (t a [], Int  N ){  // Bubble Sorting of n elements in array a [0: n-1]      For ( Int I = N; I> 1 ; I -- ) Bubble (A, I );}  Int _ Tmain ( Int Argc, _ tchar * Argv []) {  Int  A [maxnum];  For ( Int I = 0 ; I <maxnum; I ++) {A [I] = Rand () % (maxnum * 5  );}  For ( Int I = 0 ; I <maxnum; I ++ ) Cout <A [I] < "    "  ; Cout < Endl; bubblesort (A, maxnum); cout < "  After bubblesort: " < Endl;  For ( Int I = 0 ; I <maxnum; I ++ ) Cout <A [I] < "    "  ; Cin.  Get  ();  Return   0  ;} 

But the conventional bubble, whether or not the adjacent two elements have already sorted out the order, needs to bubble, this is unnecessary, and we will improve this. Design a timely terminated Bubble SortingAlgorithm:

If element swapping does not occur during a bubble process, it indicates that the array has been sorted in order and there is no need to continue the Bubble sorting. The Code is as follows:

 //  Bubblesort. cpp: defines the entry point of the console application.  //  # Include  "  Stdafx. h  "  # Include <Cmath> # Include <Iostream> Using   Namespace  STD; # Define Maxnum 20 Template <Typename T> Void Swap (T & A, T & B ){  Int T = A; = B; B = T;} Template <Typename T> Bool Bubble (t a [], Int  N ){  //  Move the largest element in array a [0: n-1] To the right through bubbling     Bool Swapped = False ; //  Exchange not occurred      For ( Int I = 0 ; I <n- 1 ; I ++ ){  If (A [I]> A [I + 1  ]) {Swap (A [I], a [I + 1  ]); Swapped =True ; //  Exchange occurred  }}  Return  Swapped;} Template <Typename T> Void Bubblesort (t a [], Int  N ){  //  Bubble Sorting of n elements in array a [0: n-1]      For ( Int I = N; I> 1 & Bubble (A, I); I --);}  Int _ Tmain ( Int Argc, _ tchar * Argv []) {  Int  A [maxnum];  For ( Int I = 0 ; I <maxnum; I ++ ) {A [I] = Rand () % (maxnum * 5  );}  For ( Int I =0 ; I <maxnum; I ++ ) Cout <A [I] < "    "  ; Cout < Endl; bubblesort (A, maxnum); cout < "  After bubblesort:  " < Endl;  For ( Int I = 0 ; I <maxnum; I ++) Cout <A [I] < "    "  ; Cin.  Get  ();  Return   0  ;} 

The improved algorithm performs the same number of comparisons as conventional bubbles in the worst case, but it is best to reduce the number of comparisons to n-1.

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.