9.2.1 Bubble Sorting Bubble Sort)
Bubble Sorting is an exchange sorting. Its basic idea is to compare the keywords of adjacent records in two or two pairs. If they are wrong, they will be exchanged. The name of this algorithm is derived from the fact that the smaller elements will gradually float at the top of the series after exchange.
1. Basic version of Bubble Sorting
# Include <stdio. h> # include <stdlib. h> void BubbleSort (int arr [], int n) {int I; int j; for (I = 0; I <n; I ++) {for (j = I + 1; j <n; j ++) {int temp; if (arr [I]> arr [j]) {temp = arr [I]; arr [I] = arr [j]; arr [j] = temp ;}}} void print (int arr [], int n) {int I; for (I = 0; I <n; I ++) {printf ("% d", arr [I]);} printf ("\ n");} int main () {int arr [] = {9, 1, 5, 8, 3, 7, 4, 6, 2 }; int n = sizeof (arr)/sizeof (arr [0]); printf ("Before sorting: \ n"); print (arr, n); BubbleSort (arr, n); printf ("sorted: \ n"); print (arr, n); system ("pause"); return 0 ;}
This code is not a standard Bubble Sorting Algorithm, because it does not meet the counterfeit sorting idea of "comparing adjacent records in two pairs". It is the simplest sort of exchange.
Idea: Compare the keyword of the 1st position with each keyword after it. If the keyword is large, it is exchanged. In this way, the keyword of the 1st position must be changed to the minimum value after a loop. The first position is no longer taken into account, so that the keywords at the first position are compared with each keyword next to it in sequence. If a keyword is entered, it is exchanged, in this way, the keyword at the first position must change to the minimum value after a loop. Loops until all keywords are sorted.
Number of times to be compared: N-1) + (n-2) +... + 2 + 1 = n (n-1)/2;
Possible maximum number of exchanges: N-1) + (n-2) +... + 2 + 1 = n (n-1)/2;
2. Bubble Sorting authentic Edition
# Include <stdio. h> # include <stdlib. h> void BubbleSort (int arr [], int n) {int I; int j; for (I = 0; I <n; I ++) {// for (j = n-2; j> = I; j --) // the smallest value floats to the top of a loop. For (j = 0; j <n-1-i; j ++) // after a loop, the maximum sink to the bottom. {Int temp; if (arr [j + 1] <arr [j]) {temp = arr [j + 1]; arr [j + 1] = arr [j]; arr [j] = temp ;}}} void print (int arr [], int n) {int I; for (I = 0; I <n; I ++) {printf ("% d", arr [I]);} printf ("\ n");} int main () {int arr [] = {9, 1, 5, 8, 3, 7, 4, 6, 2}; int n = sizeof (arr)/sizeof (arr [0]); printf ("Before sorting: \ n "); print (arr, n); BubbleSort (arr, n); printf (" sorted: \ n "); print (arr, n ); system ("pause"); return 0 ;}
After a loop, the smallest data floats slowly as a bubble, so it becomes a bubble sort.
Or the largest data sink slowly to the bottom, it can also be called Bubble sorting.
// For (j = n-2; j> = I; j --) // the smallest value floats to the top of a loop.
// For (j = 0; j <n-1-i; j ++) // after a loop, the maximum to sink to the bottom.
Number of times to be compared: N-1) + (n-2) +... + 2 + 1 = n (n-1)/2;
Possible maximum number of exchanges: N-1) + (n-2) +... + 2 + 1 = n (n-1)/2;
3. bubble sort Optimization Algorithm
If no data is exchanged after a loop, it indicates that the sequence is ordered and the subsequent loop judgment is unnecessary.
# Include <stdio. h> # include <stdlib. h> void BubbleSort (int arr [], int n) {int I; int j; bool flag = true; // optimization algorithm for (I = 0; I <n & flag; I ++) // optimization algorithm {flag = false; // optimization algorithm // for (j = n-2; j> = I; j --) // after a loop, the smallest value floats to the top. For (j = 0; j <n-1-i; j ++) // after a loop, the maximum sink to the bottom. {Int temp; if (arr [j + 1] <arr [j]) {temp = arr [j + 1]; arr [j + 1] = arr [j]; arr [j] = temp; flag = true; // optimization algorithm }}} void print (int arr [], int n) {int I; for (I = 0; I <n; I ++) {printf ("% d", arr [I]);} printf ("\ n");} int main () {int arr [] = {9, 1, 5, 8, 3, 7, 4, 6, 2}; int n = sizeof (arr) /sizeof (arr [0]); printf ("Before sorting: \ n"); print (arr, n); BubbleSort (arr, n); printf ("after sorting: \ n "); print (arr, n); system (" pause "); return 0 ;}
Optimization algorithms can avoid making meaningless loop judgments when order is already in place.
4. bubble sort Complexity Analysis
For the code analysis after optimization, if the sorting itself is in the ascending order from small to large), you only need to perform n-1 comparisons, without the need to exchange the best situation ). If it is in reverse order from large to small), we need to make n (n-1)/2 comparisons, and do n (n-1)/2 exchanges worst case ).
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282068