Basic code and optimization of Bubble sorting, and code optimization of Bubble Sorting
Bubble Sorting is an exchange sorting. Its basic idea is to compare the keywords of sentence records in two or two sequences. If the reverse order is used, it is exchanged until there is no reverse order record. Its running process is as follows (taking ascending order as an example ):
-
- Compare the elements of a sentence. If the first is bigger than the second, exchange the two of them.
- Perform the same operation on each adjacent element, from the first to the last. After this step is completed, the final element will be the largest number.
- Repeat the preceding steps for all elements except the last one.
- Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.
For example, if the array of the sequence to be sorted is 9 2 6 8 7 3 1 0 4 5, it is sorted in ascending order as the sequence 0 1 2 3 4 5 6 7 8 9, the following operations are performed according to the above bubble method:
9 2 6 8 7 3 1 0 4 5 adjacent record keywords for comparison, 9> 2 exchange get sequence: 2 9 6 8 7 3 1 0 4 5
2 9 6 8 7 3 1 0 4 5 the same, 9> 6, after the exchange: 2 6 9 8 7 3 1 0 4 5
2 6 9 8 7 3 1 0 4 5 9> 8 Exchange: 2 6 8 9 7 3 1 0 4 5
.....
2 6 8 7 3 1 0 4 9 5 9> 5 Exchange: 2 6 8 7 3 1 0 4 5 9
After comparing the N-1 times, we can see that the element 9 with the largest keyword has reached the tail end of the sequence after bubbling in ascending order. This process is called the first bubble sort. Then, the second sorting starts from the first pair of Adjacent Elements in the sequence:
2 6 8 7 3 1 0 4 5 9 --> 2 6 8 7 3 1 0 4 5 9
2 6 8 7 3 1 0 4 5 9 --> 2 6 8 7 3 1 0 4 5 9
2 6 8 7 3 1 0 4 5 9 --> 2 6 7 8 3 1 0 4 5 9
....
2 6 7 3 1 0 4 8 5 9 --> 2 6 7 3 1 0 4 5 8 9
The second sorting is complete, and the keyword 8 is raised to the second position in the last position of the sequence. We can find that the number of comparisons in the second sorting is less than that in the first sorting, and 8 is not compared with the last 9.
Repeat this process to compare the N-1 to complete the Bubble sorting of the entire sequence.
According to this idea, the corresponding code is:
Void BubbleSort1 (int array [], int arrayLength) {int I, j; for (I = 0; I <arrayLength-1; ++ I) // compare N-1 trip {for (j = 1; j <arrayLength-I; ++ j) // The Inner Loop in each comparison if (array [j] <array [J-1]) // compare Swap (array [j], array [I]); // reverse order, exchange }}
I noticed that when I was writing a bubble sort definition, I used the red font to emphasize that the bubble sort is compared with adjacent elements, as shown in the following code, although the structure is similar to the bubble, in fact, it is just a simple exchange sorting:
Void Sort (int array [], int arrayLength) {int I, j; for (I = 0; I <arrayLength-1; ++ I) {for (j = I + 1; j <arrayLength; ++ j) if (array [j] <array [I]) // non-adjacent elements compared here. This algorithm is not a bubble Swap (array [j], array [I]);}
This algorithm performs a lot of useless exchange operations, and the efficiency is very low.
The sorting algorithm can also be optimized by setting a flag in the flag field. If a certain sorting is changed, the flag is true. If a sequence is not exchanged, it indicates that the sequence has been ordered and you do not have to continue the comparison operation. In this case, the flag is false.
{Int I, j; bool flag = true; for (I = 0; I <arrayLength-1 & flag; ++ I) {flag = false; for (j = 1; j <arrayLength-I; ++ j) if (array [j] <array [J-1]) {Swap (array [j], array [J-1]); flag = true; // exchange exists, indicating that the current sequence is not yet ordered, mark as true }}}
The bubble method has another optimization, that is, to sort unordered keywords only. Suppose there are sequences of array = {, 4, 9} in ascending order, you only need to sort the unordered areas {, 0. Use a flag to record the range of unordered areas:
Void BubbleSort3 (int array [], int arrayLength) {int flag = arrayLength-1; while (flag> 0) {for (int I = 0; I <arrayLength-1; I ++) {int k = flag; flag = 0; for (int j = 0; j <k; ++ j) if (array [j] <array [j + 1]) {Swap (array [j], array [j + 1]); flag = j; // end position of the unordered Record Partition }}}}
Of course, this kind of optimization is still a bit special, but it cannot be optimized when the sequence is completely reverse.
In general, the efficiency of Bubble Sorting is relatively low, which can be used when the data size is small, Otherwise other sorting algorithms should be selected.
This article is organized and published by www.zaojuzi.com.