Reading through this article will no longer have a headache for "array sorting algorithm" (bubble sorting algorithm, direct sorting algorithm, reverse sorting algorithm)
The origin of the name of this algorithm is because the smaller the element will slowly "float" to the top of the sequence (ascending or descending order) through exchange, just like the carbon dioxide bubbles in carbonated beverages will eventually rise to the top, hence the name "bubble" Sort"
The basic idea: bubble sorting is to compare the values of two adjacent elements, if the corresponding conditions are met, exchange the element values of each other, move the smaller element to the front of the array, and move the larger element to the back of the array (that is, exchange The position of the two elements), so that the smaller element rises from the bottom to the top like a bubble
Algorithm idea: The bubbling algorithm is implemented by a double loop. The outer loop is used to control the number of sorting rounds (that is, several rounds of sorting operations). Generally, the length of the array to be sorted is "minus 1" times, because only the last cycle is left The next array element does not need to be compared, and the array has already been sorted. The inner loop is used to compare the size of each adjacent element in the array to determine whether to exchange positions. The number of comparisons and exchanges decreases with the number of sorting rounds
Compared with bubble sorting, the number of exchanges for direct selection sorting is less, so the calculation speed will be faster
Basic idea: Compare the specified sort position with other array elements separately, and exchange element values if the conditions are met
The difference with bubble sorting: instead of exchanging adjacent elements, the elements that meet the conditions are directly exchanged with the specified sorting position (for example, sorting from the last element). In this way, the sorted position gradually expands, and finally the entire array is output into a sorted format
Reorder the contents of the original array in reverse order
Basic idea: replace the last element of the array with the first element, replace the penultimate element with the second element, and so on, until all the array elements are reversed and replaced