The basic concept of BubbleSort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second trip: Still from the first logarithm
Bubble sorting refers to a computer sorting method. its time complexity is O (n ^ 2), although it is not as good as the heap sorting and fast sorting O (nlogn, the base number is 2), but there are two advantages:
- "Programming complexity" is very low and it is easy to write code;
- Stability. the stability here means that the relative sequence of the same elements in the original sequence is still maintained to the sorted sequence, but the heap sorting and quick sorting are not stable.
The basic concept of BubbleSort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
Bubble sorting is the simplest sorting algorithm with a time complexity of n square. the code is as follows: