| Common sorting methods |
Introduction to Algorithms |
Algorithm description |
Algorithm implementation |
Algorithm analysis |
| Insert algorithm |
By constructing an ordered sequence, for unsorted data, in the sorted sequence from backward to forward scan, find the appropriate location and insert. The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space that only needs O (1), so that in the backward-forward scanning process, the sorted elements need to be moved backwards and forwards to provide the insertion space for the newest elements. |
1. Starting with the first element, the element can be considered to have been sorted; 2. Remove the next element and scan from the back forward in the sequence of elements that have been sorted; 3. If the element (sorted) is greater than the new element, move the element to the next position; 4. Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element; 5. Insert the new element into the position; 6. Repeat step 2~5; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JavaScript sorting algorithm