Insertsort)
Sort by inserting the values in the sequence into a sorted sequence until the end of the sequence. Insert sorting is an improvement on Bubble sorting. It is twice faster than Bubble sorting. Generally, you do not need to use insert sorting when the data is greater than 1000, or repeat the sequence of data items that exceed 200.
1. Thoughts
Each time you select an element K and insert it to the previously sorted Part A [1... In I], K in the insert process is followed by forward and a [1... I. If a [x]> = K is found, insert K to the end of a [X]. Move the element before insertion.
2,Algorithm time complexity
In the best case, the order is in ascending order (from small to large), which only needs to be compared n times without moving. Therefore, the time complexity is O (n)
Worst case: reverse order, so that each element needs to be compared n times, a total of n elements, so the actual complexity is O (n2)
Average: O (n2)
3,Stability
Insert sorting is stable
4. Implementation
Public static void insertsort (INT [] data, int low, int high ){
For (INT I = low; I
Int temp = data [I];
Int J = I-1;
For (; j> = 0 & temp <data [J]; j --){
Data [J + 1] = data [J];
}
Data [J + 1] = temp;
}
}