BubbleSort (bubblesort)The basic concept is: compare two adjacent numbers in sequence, put the decimal points in front, and put the 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.
Bubble Sorting algorithm Implementation (Java):
Public static void bubblesort (INT [] list) {
int temp;
for (int K = 1; k for (INT I = 0; I If (list [I]> list [I + 1]) {
// swap list [I] With list [I + 1]
temp = list [I];
list [I] = list [I + 1];
list [I + 1] = temp;
}< br> }< br> }< span style =" white-space: pre ">
}
Performance Analysis of bubble sort Algorithms
If the initial status of the record sequence is "positive", the Bubble sorting process only needs to sort the sequence. In the sorting process, only n-1 exchanges are required and records are not moved. Otherwise, if the initial status of the record sequence is "backward", You need to perform n (n-1)/2 comparisons and record movement. Therefore, the total time complexity of Bubble Sorting is O (n * n ).