Bubbling method:
Basic idea: Each time the adjacent two numbers are compared, the small one to the front (that is, in ascending order). First compare the first and the 22 numbers, the small one to the front, and then the second number after the end of the comparison with the third number, the same small tune to the front. So the comparison goes on. Assuming that there are 6 numbers participating in the sort, then the first trip 22 a total of 5 times, the first trip compared to the largest number after the end. Then the second comparison, a total of 22 compared to 4 times, the largest number to the second number to the last position. A total of 5 times, 5 times than after the rendering in ascending order.
Summary rule: If there are n number, then we should make a n-1 comparison. In the first comparison, a n-1 22 comparison is performed and a n-j comparison is performed in the J-pass comparison.
Ascending: Big down, small, descending: small falling down, big up.
Implementation code:
Select the sorting method:
Basic idea (quoted from Baidu Encyclopedia): The 1th trip, in order to sort records R[1]~r[n] to select the smallest record, it and R[1] Exchange, 2nd trip, in order to sort records R[2]~r[n] Select the smallest record, and r[2] exchange; And so on, the first trip to sort records r[i]~r[ N], the smallest record is selected, and it is exchanged with r[i], so that the ordered sequence continues to grow until all sorts are completed.
The following is a simple selection of the stored state of the sort, in which the curly braces are unordered and outside the curly braces are ordered sequences: the initial sequence:
{ 49 27 65 97 76 12 38
}1th Trip: 12 and 49 swap: 12
{27 65 97 76 49 38
}2nd trip: 27 Not moving: 12 27
{65 97 76) 49 38
}3rd trip: 65 and 38 Swap: 12 27 38
{97 76 49 65
}4th trip: 97 and 49 Exchange: 12 27 38 49
{76 97 65
}5th trip: 76 and 65 Exchange: 12 27 38 49 65
{97 76
}6th trip: 97 and 76 Exchange: 12 27 38 49 65 76 97 Completion Rule Summary: If there are n number of orders, the total need to compare N, the first trip from n number of the minimum number of records to be saved in R[0], the J-trip from the number of n-j+1 to select the minimum number of records saved in R[j-1]. Implementation code:
Two kinds of ideas of simple sort--bubbling method and selection sorting method