First, the basic idea
By constructing an ordered sequence, for unsorted data, in the sorted sequence from backward to forward scan, find the corresponding position and insert.
The insertion sort is very similar to an entire poker.
At the beginning of the card, the left hand is empty, face down on the table. Next, touch a card from the table one at a time and insert it into the right position in the left hand card. In order to find the right position of this card, compare it to the hand that you already have in your hands from right to left. At any time, the cards in the left hand are orderly.
If the input array is already sorted, the insertion order is optimal, and its run time is a linear function of the input scale. If the input array is in reverse order, the worst case will occur. On average, as in the worst case, the time cost is θ (N2).
Perhaps you do not realize, but in fact your thinking process is like this: now caught a 7, put it and hand cards from right to left in turn, 7:10 small, should go to the left, 7:5 big, good, plug here. Why compare 10 and 5 to determine the position of 7? Why not compare the 4 and 2 on the left? Because there's an important premise here: The cards in hand are already in order. Now I inserted 7, the hand is still in the order of the cards, the next time to catch the card can also be inserted by this method. The same is true for programming an array for insertion, but unlike inserting a poker, it is not possible to insert another unit between two adjacent storage cells, so the data after the insertion point is moved back one cell at a later step.
Second, the algorithm description
Assuming that n is the length of the array,
First, assume that the first element is placed in the correct position, so that only the remaining elements are sorted from within the 1-n-1 range. For each traversal, the elements from the 0-i-1 range have been sequenced,
The task for each traversal is to position the element at position I at the correct position within the sub-list from 0 to I by scanning the previously sorted sub-list.
Copy Arr[i] As a temporary element named target.
Scan down the list to compare the target value to the size of arr[i-1], arr[i-2], and so on.
This comparison stops at the first element (Arr[j]) that is less than or equal to the target value, or stops at the beginning of the list (j=0).
When Arr[i] is smaller than any previously ordered element, the latter condition (j=0) is true,
Therefore, this element occupies the first position of the new sorted sub-list.
During a scan, each element that is greater than the target value is sliding one position (arr[j]=arr[j-1]) to the right.
Once the correct position J is determined,
The target value (i.e. the original arr[i]) is copied to this location.
Unlike the selection sort, the Insert sort slides the data to the right and does not perform the interchange.
Three, sample code
12345678910111213141516171819202122 |
public
static
void
InsertSort(
int
[] arr)
{
int
i, j;
int
n = arr.Length;
int
target;
//假定第一个元素被放到了正确的位置上
//这样,仅需遍历1 - n-1
for
(i = 1; i < n; i++)
{
j = i;
target = arr[i];
while
(j > 0 && target < arr[j - 1])
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = target;
}
}
|
Iv. Efficiency Analysis
Stability
Space complexity O (1)
Time complexity O (n2)
Worst case: Reverse order, need to move n (n-1)/2 elements
Best case: Positive order, no moving element required
The best time to insert sort efficiency is O (n) when the array is sorted or "approximate";
The insertion sort worst-case run time and average run time are O (N2).
In general, the insertion sort renders the best performance in two sorting algorithms.
For lists with fewer elements, such as n<=15, the two-time algorithm is very effective.
When the list has been sorted, the insertion sort is the linear algorithm O (n).
The insertion sort is still a linear algorithm when the list is "approximate sort".
When many elements of a list are already in the correct position, the "approximate sort" condition appears.
By using an O (nlog2n) efficiency algorithm (such as quick sort) to sort the array in parts,
Then select Sort, and some advanced sorting algorithms are implemented in this way.
Understanding of Insert ordering in Java and examples