First: the most basic solution
void bubble_sort1( int a[], int size ){bool swapped = true;int n = size;while ( swapped ) {swapped = false;for ( int i = 0; i < n - 1; i++ ) {if ( a[i] > a[i+1] ) {int tmp;tmp = a[i];a[i] = a[i+1];a[i+1] = tmp;swapped = true;}//if}//for}//while}
The first solution is to place the largest I + 1 element from the first element each time, but compare all n elements each time, obviously, each time compared with the previously arranged elements is redundant. Lead to solution 2: N minus 1 after each sorting.
The second type is the same as the Bubble sorting of the layer-2 for loop.
void bubble_sort2( int a[], int size ){bool swapped = true;int n = size;while ( swapped ) {swapped = false;for ( int i = 0; i < n - 1; i++ ) {if ( a[i] > a[i+1] ) {int tmp;tmp = a[i];a[i] = a[i+1];a[i+1] = tmp;swapped = true;}//if}n = n - 1;}//while}
The above solution 1 and 2 is the Bubble sorting of the double for loop we often write, as shown below:
Below is the Bubble sorting we usually write (from Wikipedia)
void bubble_sort(int a[], const int size){ bool flag = true; int temp = 0; /* Temporary value for swapping two elements */ for (int i = 0; i < size - 1; i ++) { flag = true; for (int j = 0; j < size - i - 1; j ++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; flag = false; } // end if } // end for j = ... if (flag == true) break; } // end for i = ...}
Then there is the third solution: the principle of this solution is to reduce the number of orders each time. Because every time we exchange two numbers a [I], a [I + 1, this is because a [I] <A [I + 1] always has the element value at the position where I is smaller than the element following it, and thus the value of N can be reduced.
Exact description:
More generally, it can happen that more than one element is placed in their final position on a single pass. in particle, after every pass, all elements after the last swap are sorted, and do not need to be checked again. this allows us to skip over a lot
Of the elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in SWAp counts), and adds very little complexity because the new Code subsumes the "swapped" variable:
To accomplish this in pseudo docode we write the following:
Void bubble_sort (int A [], const int size) {int n = size; int newn = 0; int swap = 0; while (n) {newn = 0; for (INT I = 0; I <n-1; I ++) {if (a [I]> A [I + 1]) {/* too slow a [I] = A [I] + A [I + 1]; A [I + 1] = A [I]-A [I + 1]; A [I] = A [I]-A [I + 1]; */int tmp; TMP = A [I]; A [I] = A [I + 1]; A [I + 1] = TMP; newn = I + 1 ;}}n = newn ;}}
Although bubble_sort is the simplest Sorting Algorithm in terms of understanding and implementation(N2)The time complexity means that when lists has more elements, the efficiency of this algorithm is very low. Even in
Those simple o(N2)In sort algorithms with time complexity, such as insert sorting is generally considered to be better than Bubble sorting.
Due to the simplicity of Bubble sorting, it is often used as an introduction to the concept of algorithms ..... Omitted ..
This article is only intended to be written when reading the introduction of Data Structure and algorithm analysis. Readers should still master other advanced sorting algorithms.
Reference: http://en.wikipedia.org/wiki/Bubble_sort