Insertion sort

Insertion sort

Infobox Algorithm
class=Sorting algorithm


data=Array
time="О(n²)"
space="О(n)" total, "O(1)" auxiliary
optimal=Not usually

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort, but it has various advantages:

* Simple to implement
* Efficient on (quite) small data sets
* Efficient on data sets which are already substantially sorted: it runs in O("n" + "d") time, where d is the number of inversions
* More efficient in practice than most other simple O("n"2) algorithms such as selection sort or bubble sort: the average time is "n"2/4 and it is linear in the best case
* Stable (does not change the relative order of elements with equal keys)
* In-place (only requires a constant amount O(1) of extra memory space)
* It is an online algorithm, in that it can sort a list as it receives it.

Algorithm

In abstract terms, every iteration of an insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list, until no elements are left in the input. The choice of which element to remove from the input is arbitrary and can be made using almost any choice algorithm.

Sorting is typically done in-place. The resulting array after "k" iterations contains the first "k" entries of the input array and is sorted.In each step, the first remaining entry of the input is removed, inserted into the result at the right position, thus extending the result:

becomes:

with each element > "x" copied to the right as it is compared against "x".

The most common variant, which operates on arrays, can be described as:

# Suppose we have a method called "insert" designed to insert a value into a sorted sequence at the beginning of an array. It operates by starting 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. It has the side effect of overwriting the value stored immediately after the sorted sequence in the array.
# To perform insertion sort, start at the left end of the array and invoke "insert" to insert each element encountered into its correct position. The ordered sequence into which we insert it is stored at the beginning of the array in the set of indexes already examined. Each insertion overwrites a single value, but this is okay because it's the value we're inserting.

A simple pseudocode version of the complete algorithm follows, where the arrays are zero-based and the for loop includes both the top and bottom limits (like in Pascal): insertionSort(array A) for i = 1 to length [A] -1 do begin value = A [i] j = i-1 while j ≥ 0 and A [j] > value do begin A [j + 1] = A [j] j = j-1 end A [j+1] = value end

Good and bad input cases

In the best case of an already sorted array, this implementation of insertion sort takes O("n") time: in each iteration, the first remaining element of the input is only compared with the last element of the sorted subsection of the array. This same case provides worst-case behavior for non-randomized and poorly implemented quicksort, which will take O("n"2) time to sort an already-sorted list.

The worst case is an array sorted in reverse order, as every execution of the inner loop will have to scan and shift the entire sorted section of the array before inserting the next element. Insertion sort takes O("n"2) time in this worst case as well as in the average case, which makes it impractical for sorting large numbers of elements. However, insertion sort's inner loop is very fast, which often makes it one of the fastest algorithms for sorting small numbers of elements, typically less than 10 or so.

Comparisons to other sorts

Insertion sort is very similar to selection sort. Just like in selection sort, after "k" passes through the array, the first "k" elements are in sorted order. For selection sort, these are the "k" smallest elements, while in insertion sort they are whatever the first "k" elements were in the unsorted array. Insertion sort's advantage is that it only scans as many elements as it needs to in order to place the "k" + 1st element, while selection sort must scan all remaining elements to find the absolute smallest element.

Simple calculation shows that insertion sort will therefore usually perform about half as many comparisons as selection sort. Assuming the "k" + 1st element's rank is random, it will on the average require shifting half of the previous "k" elements over, while selection sort always requires scanning all unplaced elements. If the array is not in a random order, however, insertion sort can perform just as many comparisons as selection sort (for a reverse-sorted list). It will also perform far fewer comparisons, as few as "n" - 1, if the data is pre-sorted, thus insertion sort is much more efficient if the array is already sorted or "close to sorted."

While insertion sort typically makes fewer comparisons than selection sort, it requires more writes because the inner loop can require shifting large sections of the sorted portion of the array. In general, insertion sort will write to the array O("n"2) times while selection sort will write only O("n") times. For this reason, selection sort may be better in cases where writes to memory are significantly more expensive than reads, such as EEPROM or Flash memory.

Some divide-and-conquer algorithms such as quicksort and mergesort sort by recursively dividing the list into smaller sublists which are then sorted. A useful optimization in practice for these algorithms is to switch to insertion sort for small sublists on which insertion sort outperforms the more complex algorithms. The size of list for which insertion sort has the advantage varies by environment and implementation, but is typically around 8 to 20 elements.

Variants

D.L. Shell made substantial improvements to the algorithm, and the modified version is called Shell sort.It compares elements separated by a distance that decreases on each pass. Shell sort has distinctly improved running times in practical work, with two simple variants requiring O("n"3/2) and O("n"4/3) time.

If comparisons are very costly compared to swaps, as is the case for example with string keys stored by reference or with human interaction (such as choosing one of a pair displayed side-by-side), then using binary insertion sort can be a good strategy. Binary insertion sort employs binary search to find the right place to insert new elements, and therefore performs lceil log_2(n!) ceil comparisons in the worst case, which is Θ("n" log "n"). The algorithm as a whole still takes Θ("n"2) time on average due to the series of swaps required for each insertion, and since it always uses binary search, the best case is no longer Ω("n") but Ω("n log n").

To avoid having to make a series of swaps for each insertion, we could instead store the input in a linked list, which allows us to insert and delete elements in constant time. Unfortunately, binary search on a linked list is impossible, so we still spend O("n"2) time searching. If we instead replace it by a more sophisticated data structure such as a heap or binary tree, we can significantly decrease both search and insert time. This is the essence of heap sort and binary tree sort.

In 2004, Bender, Farach-Colton, and Mosteiro published a new variant of insertion sort called "library sort" or "gapped insertion sort" that leaves a small number of unused spaces ("gaps") spread throughout the array. The benefit is that insertions need only shift elements over until a gap is reached. Surprising in its simplicity, they show that this sorting algorithm runs with high probability in O("n" log "n") time. [http://citeseer.ist.psu.edu/bender04insertion.html]

External links

* [http://tide4javascript.com/?s=Insertion Analyze Insertion Sort in an online Javascript IDE]
* [http://web.engr.oregonstate.edu/~minoura/cs162/javaProgs/sort/InsertSort.html An animated Java applet showing a step-by-step insertion sort.]
* [http://literateprograms.org/Category:Insertion_sort Literate implementations of insertion sort in various languages] on LiteratePrograms
* Pointers to [http://web-cat.cs.vt.edu/AlgovizWiki/InsertionSort insertion sort visualizations]
* [http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html Page with visual demonstrations of sorting algorithms] , all the implementation in Java.
* [http://vision.bc.edu/~dmartin/teaching/sorting/anim-html/insertion.html A graphical demonstration and discussion of insertion sort]
* [http://coderaptors.com/?InsertionSort A colored graphical Java applet] which allows experimentation with initial state and shows statistics

References

* Donald Knuth. "The Art of Computer Programming", Volume 3: "Sorting and Searching", Second Edition. Addison-Wesley, 1998. ISBN 0-201-89685-0. Section 5.2.1: Sorting by Insertion, pp.80–105.
* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. "Introduction to Algorithms", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 2.1: Insertion sort, pp.15–21.


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Insertion Sort — Insertionsort (engl. insertion – das Einfügen, sort – sortieren) ist ein einfaches stabiles Sortierverfahren. Es ist weit weniger effizient als andere anspruchsvollere Sortierverfahren. Dafür hat es jedoch folgende Vorteile: Es ist einfach zu… …   Deutsch Wikipedia

  • Insertion sort — Insertionsort (engl. insertion – das Einfügen, sort – sortieren) ist ein einfaches stabiles Sortierverfahren. Es ist weit weniger effizient als andere anspruchsvollere Sortierverfahren. Dafür hat es jedoch folgende Vorteile: Es ist einfach zu… …   Deutsch Wikipedia

  • Insertion — may refer to: *Insertion (anatomy), the point of attachment of a tendon or ligament onto the skeleton or other part of the body *Insertion (genetics), the addition of DNA into a genetic sequence *Insertion loss, in electronics *Insertion sort, a… …   Wikipedia

  • Insertion Cotraductionnelle Des Protéines Membranaires — L insertion cotraductionnelle des protéines membranaires permet l insertion d une protéine dans une membrane au cours de sa traduction. Ce processus permet uniquement à la formation de protéines à hélices α transmembranaires. L insertion… …   Wikipédia en Français

  • Insertion cotraductionnelle des proteines membranaires — Insertion cotraductionnelle des protéines membranaires L insertion cotraductionnelle des protéines membranaires permet l insertion d une protéine dans une membrane au cours de sa traduction. Ce processus permet uniquement à la formation de… …   Wikipédia en Français

  • Insertion cotraductionnelle des protéines membranaires — L insertion cotraductionnelle des protéines membranaires permet l insertion d une protéine dans une membrane au cours de sa traduction. Ce processus permet uniquement à la formation de protéines à hélices α transmembranaires. L insertion… …   Wikipédia en Français

  • Insertion sequence — An insertion sequence (also known as an IS, an insertion sequence element, or an IS element) is a short DNA sequence that acts as a simple transposable element. Insertion sequences have two major characteristics: they are small relative to other… …   Wikipedia

  • Shell sort — is a sorting algorithm that is a generalization of insertion sort, with two observations: *insertion sort is efficient if the input is almost sorted , and *insertion sort is typically inefficient because it moves values just one position at a… …   Wikipedia

  • Selection sort — Infobox Algorithm class=Sorting algorithm data=Array time= О(n²) space= О(n) total, O(1) auxiliary optimal=Not usuallySelection sort is a sorting algorithm, specifically an in place comparison sort. It has O( n 2) complexity, making it… …   Wikipedia

  • Bubble sort — Infobox Algorithm class=Sorting algorithm data=Array time= О(n²) space= О(n) total, O(1) auxiliary optimal=NoBubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”