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.