Overview
The basic operation of a direct insert sort (straight insertion sort) is to insert a record into an ordered table that is already sorted, resulting in a new, sequential table with a 1 increase in the number of records.
– "Big talk data Structure"
Copyright notice
Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
This article Coding-naga
Published: March 24, 2016
Original link: http://blog.csdn.net/lemon_tree12138/article/details/50968422
Source: CSDN
More information: Classification >> arithmetic and mathematics
Directory
- Overview
- Copyright notice
- Directory
- Algorithm principle Analysis
- Algorithm steps
- Logical implementation
- Analysis of complexity
- Ref
- GitHub Source Download
Algorithm principle Analysis
As we can see from the above overview, the process of sorting the arrays requires two sequences to complete.
A sequence of unordered orders to be sorted, and a sorted ordered sequence. All we have to do now is to insert the chaotic elements from the chaotic sequence into an ordered sequence. Just like this:
However, there are still some not very good places, the more obvious place is that we need to add an additional auxiliary array. If the data to be sorted is larger, then the strategy of adding the auxiliary array may not be used.
It is not difficult to imagine that in the original array there is such an equation: the whole sequence = ordered sequence + chaotic sequence
In other words, we can divide the current array into a sequence on the left and a disorderly order on the right.
This removes the first element from the sequence and inserts it from the sequence. Until the sequence is ordered as a whole. For specific steps, see the algorithm steps below.
Algorithm steps
- The No. 0 element in the default sequence is ordered (because there is only one element a[0], which is naturally ordered);
- Starting with the Subscript 1 (subscript starting from 0), take the element at the current subscript i position a[i] and save to a temporary variable waitinsert;
- The first half of the ordinal sequence of the loop traversal, and compared with Waitinsert, until the encounter a smaller than Waitinsert element (the default is from small to large sort), at this time the subscript is J, then now as long as the a[j+1] to be assigned waitinsert;
- The subscript I of the element to be inserted goes backward one position;
- Repeat steps 2nd through 4th until the elements in the sequence are all inserted into the ordered sequence;
- After the above 5 steps, the whole sequence must be ordered and sorted.
Logical implementation
/* * Core module of sorting algorithm * * @param array * Array to be sorted */ private void sortcore (int [] array) {int arraySize = Array.Length; for (int i = 1 ; i < arraySize; i++) {int j = i; int waitinsert = array[i]; while (J > 0 && Waitinsert < array[j-1 ]) {Array[j] = array[j-1 ]; j--; } Array[j] = Waitinsert; } }
Analysis of complexity
| Sorting methods |
Complexity of Time |
Complexity of space |
Stability |
Complexity |
| Average situation |
Worst case scenario |
Best case |
| Insert Sort |
O (N2) |
O (N2) |
O (N) |
O (N) |
Stability |
Simple |
The worst case and average situation here can be seen from the code, because there are two nested for loops. So where's the best of it? This is for an ordered sequence, do not need to exchange, just compare n times, so the best time here is the complexity of O (n).
Ref
- "Big Talk Data Structure"
GitHub Source Download
Https://github.com/William-Hai/ArraySortAlgorithm/blob/master/src/org/algorithm/array/sort/impl/InsertSort.java
Sorting algorithm series: inserting sorting algorithms