-
Just talk about the idea, if 5 numbers 7 2 3 4 9 first the first and the second comparison, if the former is large, then exchange, small is unchanged, such as 7 and 2 exchange and then take the second and third comparison, 7 and 3 comparison, 7 large, and 3 exchange, the third position is 7, and then the third position and four comparison, 7 is greater than 4, exchange, the fourth position is 7, 7 is less than 9, do not exchange, the sequence is 2 3 4 7 9 second round, 2 and 3 The comparison position remains the same, 3 and 4 The comparison position remains the same, 4 and 7 The comparison position remains the same, At the end of the round, the position remains unchanged in 2 and 3 comparisons,。。 I know the end of the competition.
-
Method 2: Record the swap position, you can omit unnecessary comparisons on the next trip (if the next few elements are already ordered, the second trip does not need to compare these elements);
Method 3: Bubble in both directions, find the maximum in the forward direction (start the comparison from the minimum element swap position, find the largest element and record the swap position), and find the minimum in the reverse direction (start the comparison from the largest element swap position, find the smallest element and record the swap bit).
Bubbling sorting is one of the basic sorting algorithms for comparison, the idea is that adjacent elements are compared in pairs, the larger number sinks, and the smaller number rises, so that after a comparison, the maximum (small) value will be arranged at one end, and the whole process is like a bubble rising, so it is called bubbling sorting.
-
Bubbling sort is the easiest sorting method and is easy to understand. Although it has more calculation steps and is not the fastest, it is the most basic and must be mastered by beginners.
The principle of bubble sorting is: from left to right, adjacent elements are compared. Each round of comparison finds the largest or smallest one in the sequence. This number will pop up from the far right of the sequence.
For example, after the first round of comparison, the largest number in all numbers will float to the far right; After the second round of comparison, the second highest number of all numbers will float to the second-to-last position......In this way, round by round, the comparison is carried out, and finally the order is achieved from small to large.
For example, sort the following sequence from smallest to largest:
Round 1: When comparing, the nth comparison in each round is a comparison of the nth element and the n+1st element in the new sequence (assuming n starts at 1).
Round 2: Round 3:
At this point, the third round is over. The result of the third round is to find the third largest number in the sequence and float to the third position on the far right.
Round 4:From this example, it can also be concluded that if there are n data, then only n 1 rounds need to be compared, because each round of sorting, the largest number can be found, so each round of comparison is n-1-i times (i is the number of numbers on the right that have been determined, that is, the number of rounds that have been performed).
Write a program below:
The output is: 0 1 2 3 4 5 6 7 8 9
-
The order of the bubbling method is as follows:
Bubbling sorts by repeatedly visiting the columns of elements to be sorted, comparing two adjacent elements in turn, and swapping them if the order (e.g. from largest to smallest, bumper with the first letter from z to a) is wrong. The work of visiting an element is repeated until there are no neighbors to swap, i.e. the element column has been sorted.
The name of this algorithm comes from the fact that the smaller elements slowly "float" to the top of the sequence (ascending or descending) through exchange, just as the bubbles of carbon dioxide in carbonated drinks eventually rise to the top, hence the name "bubbling sorting".
Bubbling is the process of putting small elements in front or large elements in the back. The comparison is the comparison of two adjacent elements, and the exchange also takes place between these two laughing pure elements.
So, if the two elements are equal, they will not be exchanged again; If two equal elements are not adjacent, then even if the two are adjacent by the previous pairwise exchange, they will not be swapped at this time, so the order of the same elements does not change, so bubble sorting is a stable sorting algorithm.
Algorithm Principle:
1. Compare adjacent elements. If the first one is bigger than the second, swap both of them. Do the same work for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the final element should be the largest.
2. Repeat the above steps for all elements, except for the last one. Repeat the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
When bubbling sorted.
for j = 0 to n - 2 >>>More
Bubbling sort is the least efficient. Juvenile! You can quickly sort it, and you will understand it in minutes.
The central idea of bubbling ordering is to start at the head of the unordered sequence, compare them in pairs, swap positions according to size, and finally swap the largest (small) data element to the end of the unordered queue, thus becoming part of the ordered sequence; Continue the process next time until all the data elements are ordered. The core of the algorithm is to select the largest (smallest) data element in the remaining disordered sequence and put it at the end of the queue by comparing the positions in pairs at a time. >>>More
First of all, because this array only has 10 data, and the subscript starts from 0 and ends at i=9. >>>More
The precepts are: Law Injury", Intelligence, Rapid, Crit, and Blue Spirit. >>>More