PreviousArticleYou have already introduced two basic sorting methods: insert sorting and select sorting. Today we will introduce another sorting method, Bubble sorting.
Thoughts
Like the two sorting methods described earlier, Bubble Sorting is also one of the simplest and most basic sorting methods. The idea of Bubble Sorting is very simple, that is, to compare the size of Adjacent Elements and move the smallest elements forward and backward, just like bubbles in the water. The smallest elements move several times, will eventually float on the water.
For example, the following data is shown:
2 7 4 69 1First, compare the last two digits and find that 1 is smaller than 9, so move forward
2 7 46 19Then compare 6 and 1
2 74 16 9 continue to move forward, then 4 and 1
27 14 6 9 7 and 1 Comparison
2 17 4 6 9 2 and 1
12 7 46 9At this point, the first bubble process is completed, and the smallest element 1 is moved to the first one, so it is no longer involved in the subsequent sorting process. The next bubble process is the same as 6 and 9, and so on. The final result is obtained.
Code
Cout < " Bubble sort: " <Endl;
Printline ( " Before sort: " , V );
For ( Int I = 0 ; I <v. Size (); I ++ ){
Int Temp = 0 ;
For ( Int J = V. Size ()- 1 ; J> 0 ; J --){
If (V [J] <V [J- 1 ]) {
Temp = V [J];
V [J] = V [J- 1 ];
V [J- 1 ] = Temp;
}
}
}
Printline ( " After sort: " , V );
Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting. This documentAlgorithmThe time complexity is O (n * n), which cannot be regarded as an efficient algorithm.
Careful analysis is not difficult to find that this algorithm still has room for optimization. If no bubble position exchange is found in a certain sort, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to false before each sort starts. If an exchange occurs during the sorting process, set it to true. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting. This reduces unnecessary comparisons. The Code is as follows:
Int Bubble_sort (vector < Int > & V ){
Cout < " Bubble sort: " <Endl;
Printline ( " Before sort: " , V );
Bool exchange;
For ( Int I = 0 ; I <v. Size (); I ++ ){
Int Temp = 0 ;
Exchange = false;
For ( Int J = V. Size ()- 1 ; J> 0 ; J --){
If (V [J] <V [J- 1 ]) {
Temp = V [J];
V [J] = V [J- 1 ];
V [J- 1 ] = Temp;
Exchange = true;
}
}
If (! Exchange ){
Break;
}
}
Printline ( " After sort: " , V );
}
The above is a simple description and explanation of the Bubble sorting, hoping to inspire and help you.
We will introduce some efficient sorting algorithms later.