1. Bubble sortBasic idea: For each order of the trip, start with the first number, and then compare the size of the previous number to the next.If the previous number is larger than the last one, it is exchanged. After that, the largest number will appear at the bottom of the last position.In the second round, the last number is removed, the number of the first n-1 is followed by the above steps to find the maximum number, which will appear in the penultimate position.After the n-1 round, the sorting is completed., for example: Bubble sort 1,5,2,3,9,8,6,First trip:, 1<5, no swap, 5>2, Exchange 1 2 5, 3, 9 8,65>3 Exchange,
1 2 3 5 9 8,65<9 does not change, 9>8 exchange,
1 2 3 5 8 9,6,9>6 Exchange,1 2 3 5 8 6 9 9 Position fixed
Second trip: 1<2 do not change, 2<3 do not change, 3<5 do not change, 5<8 do not change, 8>6 Exchange 1 2 3 5 6 8 9 8 position fixed
Iterate in sequence
N number of worst-case comparisons: 1+2+......+n-1
2. Insert Sort
Basic idea: The keywords to be inserted into the record r[i] are compared from right to left with the keyword in the ordered area recorded R[j] (j=i-1, I-2, ...., 1):
If R[J] has a keyword greater than r[i], then r[j] moves back one position
If the keyword of r[j] is less than or equal to R[i], then the lookup process ends and J + 1 is the r[i] insert position
Example: Insert sort: 34,8,64,51,32,21
Suppose there were numbers at first: 8<34 8 34
64>34 8 34 64
51<64 51>34 8 34 51 64
32<64 32<51 32<34 32>8 8 32 34 51 64
21<64 21<51 21<34 21<32 21>8 8 21 32 34 51 64 insert and Bubble, each time 2 adjacent elements are exchanged, just eliminate 1 reverse pairs. (The following table I<j, if A[I]>A[J] is called (I,J) is a pair of reverse pairs) if the sequence is basically orderly, the insertion sort is simple and efficient. A sequence of any n different elements has an average of n (N-1)/4 reverse order for any algorithm that is ordered only by exchanging adjacent elements, with an average time complexity of US-American Pavilion (n*n)
3. Hill sort overcomes the problem that the first two sorts only exchange adjacent elements for sorting at a time.
basic idea: first divides the entire backlog sequence into several sub-sequences (consisting of elements separated by an "increment d"), sorts the elements that are separated by D, and then reduces the increments in order to sort the elements in the whole sequence (the increments are small enough). And then a direct insert sort of the whole element.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sort by bubble sort, insert sort and hill sort