Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Insertion sort
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Algorithm== [[File:Insertion-sort-example-300px.gif|300px|thumb|right|A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (red) is removed from the "not yet checked for order" input data and inserted in-place into the sorted list.]] Insertion sort [[Iteration|iterates]], consuming one input element each repetition, and grows a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the correct location within the sorted list, and inserts it there. It repeats until no input elements remain. Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At each array-position, it checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position. The resulting array after ''k'' iterations has the property where the first ''k'' + 1 entries are sorted ("+1" because the first entry is skipped). In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result: [[Image:insertionsort-before.png|Array prior to the insertion of x]] becomes [[Image:insertionsort-after.png|Array after the insertion of x]] with each element greater than ''x'' copied to the right as it is compared against ''x''. The most common variant of insertion sort, which operates on arrays, can be described as follows: # Suppose there exists a function called ''Insert'' designed to insert a value into a sorted sequence at the beginning of an array. It operates by beginning at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. The function has the side effect of overwriting the value stored immediately after the sorted sequence in the array. # To perform an insertion sort, begin at the left-most element of the array and invoke ''Insert'' to insert each element encountered into its correct position. The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined. Each insertion overwrites a single value: the value being inserted. [[Pseudocode]] of the complete algorithm follows, where the arrays are [[Zero-based numbering|zero-based]]:<ref name="pearls">{{cite book |last=Bentley |first=Jon |date=2000 |title=Programming Pearls |edition=2nd |chapter=Column 11: Sorting |publisher=ACM Press / Addison-Wesley |isbn=978-0-201-65788-3 |oclc=1047840657 |pages=115β116 |chapter-url=https://books.google.com/books?id=kse_7qbWbjsC&pg=PA116}}</ref> <!-- *************************************** NOTE TO WOULD-BE BUG FIXERS: The variable i is meant to range over all but the first element of A. This is intentional. If you think you see a bug, please double- and triple-check your logic (and consider implementing and testing your idea), then discuss your fix on the Talk page, *BEFORE* changing this code. *************************************** --> i β 1 '''while''' i < length(A) j β i '''while''' j > 0 '''and''' A[j-1] > A[j] '''swap''' A[j] and A[j-1] j β j - 1 '''end while''' i β i + 1 '''end while''' The outer loop runs over all the elements except the first one, because the single-element prefix <code>A[0:1]</code> is trivially sorted, so the [[Invariant (computer science)|invariant]] that the first <code>i</code> entries are sorted is true from the start. The inner loop moves element <code>A[i]</code> to its correct place so that after the loop, the first <code>i+1</code> elements are sorted. Note that the <code>'''and'''</code>-operator in the test must use [[short-circuit evaluation]], otherwise the test might result in an [[Bounds checking|array bounds error]], when <code>j=0</code> and it tries to evaluate <code>A[j-1] > A[j]</code> (i.e. accessing <code>A[-1]</code> fails). After expanding the <code>'''swap'''</code> operation in-place as <code>x β A[j]; A[j] β A[j-1]; A[j-1] β x</code> (where <code>x</code> is a temporary variable), a slightly faster version can be produced that moves <code>A[i]</code> to its position in one go and only performs one assignment in the inner loop body:<ref name="pearls"/> i β 1 '''while''' i < length(A) x β A[i] j β i '''while''' j > 0 '''and''' A[j-1] > x A[j] β A[j-1] j β j - 1 '''end while''' A[j] β x<ref>{{Introduction to Algorithms|edition=3|chapter=Section 2.1: Insertion sort|pages=16β18|mode=cs2}}. See page 18.</ref> i β i + 1 '''end while''' The new inner loop shifts elements to the right to clear a spot for <code>x = A[i]</code>. The algorithm can also be implemented in a recursive way. The recursion just replaces the outer loop, calling itself and storing successively smaller values of ''n'' on the stack until ''n'' equals 0, where the function then returns up the call chain to execute the code after each recursive call starting with ''n'' equal to 1, with ''n'' increasing by 1 as each instance of the function returns to the prior instance. The initial call would be ''<code>insertionSortR(A, length(A)-1)</code>''. '''function''' insertionSortR(array A, int n) '''if''' n > 0 insertionSortR(A, n-1) x β A[n] j β n-1 '''while''' j >= 0 '''and''' A[j] > x A[j+1] β A[j] j β j-1 '''end while''' A[j+1] β x '''end if''' '''end function''' It does not make the code any shorter, it also does not reduce the execution time, but it increases the additional memory consumption from {{math|O(1)}} to {{math|O(N)}} (at the deepest level of recursion the stack contains {{math|N}} references to the {{code|A}} array, each with accompanying value of variable {{code|n}} from {{math|N}} down to 1).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)