Insert sorting for algorithm sorting
Basic Ideas
In general, insert sorting is implemented on the array using in-place. The specific algorithm is described as follows:
Starting from the first element, this element can be considered to have been sorted
Extracts the next element and scans it forward from the back in the sorted element sequence.
If the element (sorted) is greater than the new element, move the element to the next position.
Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.
Insert the new element to this position.
Repeat Step 2 ~ 5
Dynamic demonstration
Sort by insert into a column of numbers
Time Complexity Analysis
If the target is to sort the sequences of n elements in ascending order, there are best cases and worst cases of adopting insert sorting. The best case is that the sequence is already in ascending order. In this case, the comparison operation needs to be performed (n-1) times. The worst case is that the sequence is sorted in descending order. At this time, we need to compare n (n-1)/two times. The value assignment operation for insert sorting is the number of comparison operations minus (n-1. On average, the complexity of the insert sorting algorithm is O (n2 ). Therefore, insert sorting is not suitable for sorting applications with large data volumes. However, if the amount of data to be sorted is small, for example, if the order is smaller than, insertion sorting is still a good choice. Insert sorting is also widely used in industrial databases. in STL sort algorithm and stdlib qsort algorithm, insert sorting is used as a supplement to quick sorting, used to sort a small number of elements (usually eight or less ).
Algorithm Improvement
1. If the cost of the compare operation is higher than that of the switch operation, you can use the binary search method to reduce the number of compare operations. This algorithm can be considered as a variant of insert sorting, called Binary Search sorting.
2. Search for comparison operations and record moving operations Alternately
Specific Practices:
Record the keywords of the record R [I] from right to left and record R [j] (j = I-1, I-2 ,..., 1) keyword comparison:
① If the keyword of R [j] is greater than the keyword of R [I], the R [j] is moved back to a position;
② If the keyword of R [j] is less than or equal to the keyword of R [I], the search process ends, and j + 1 is the insert position of R [I.
Records with larger keywords than those of R [I] have been moved backward, so the position of j + 1 has been vacated, you only need to insert R [I] directly to this position to complete the sort of insertion directly.
Algorithm C implementation
[Cpp]
Void insertion_sort (char array [], int first, int last)
{
Int I, j;
Int temp;
For (I = first + 1; I <= last; I ++)
{
Temp = array [I];
J = I-1;
// Compare with the sorted number one by one. If the value is greater than temp, the number is moved back.
While (j> = first) & (array [j]> temp) // when first = 0, j loops to-1, because [[Short Circuit evaluation], array [-1] is not computed.
{
Array [j + 1] = array [j];
J --;
}
Array [j + 1] = temp; // place the sorted number in the correct position
}
}