[Home]History of Insertion sort

HomePage | Recent Changes | Preferences

Revision 8 . . December 16, 2001 11:52 am by Carey Evans [rewrite with generic insertion sort, and previous article as straight insertion sort; needs checking]
Revision 7 . . (edit) December 16, 2001 8:17 am by Carey Evans [once more...]
Revision 6 . . (edit) December 16, 2001 8:13 am by Carey Evans [off by one, aagh]
Revision 5 . . December 16, 2001 8:07 am by Carey Evans [rewrite code with useful variable names, comments, and reflecting intent of algorithm better; probably faster too]
Revision 4 . . December 16, 2001 6:51 am by Carey Evans [simplify Python "pseudo-code"]
Revision 3 . . (edit) December 11, 2001 5:24 pm by Hannes Hirzel [/Talk added]
Revision 2 . . October 30, 2001 4:10 am by Kragen [Explained O(n) best case.]
  

Difference (from prior major revision) (author diff)

Changed: 1c1
Insertion sort is a sort algorithm very similar to bubble sort; the two even compare and exchange exactly the same pairs of elements, although in a different chronological order.
Insertion sort is a simple sort algorithm where the result is built up one entry at a time.

Changed: 3c3,11
The algorithm is as follows:
In abstract terms, each iteration of an insertion sort removes an element from the input data, inserting it into the correct sequence in the result, until no elements are left in the input.
The choice of which element to remove from the input is arbitrary.

The implementation of the result has the greatest impact on the time taken, so an ordered tree data structure with efficient inserts and in-order traversal will give good results. Specific implementations of insertion sort using these data structures may have their own names.

Straight insertion sort




Straight insertion sort is a very simple implementation of insertion sort.
Sorting is done in-place. The result array after n iterations is the first n entries of the input array, where the first remaining entry of the input is removed each time, and the result extends into the input when this entry is inserted:

Changed: 6c14,26
def insertionsort(array):
_____ result ______ ______ input ______
| < x | > x | x | ... |
</pre>
becomes:

________ result _______ ____ input ____
| < x | x | > x | ... |


The algorithm can be written as follows:

<pre>
def straightinsertionsort(array):

Added: 19a40
Straight insertion sort is very similar to bubble sort.

Changed: 22,24c43,45
Insertion sort (and bubble sort) take O(n2) time in the average, best, and worst cases, which makes them impractical for sorting large numbers of elements; however, their inner loops are very fast, which often makes them the fastest algorithms around for sorting small numbers of elements, typically less than 10 or so. Quicksort works by dividing the array to be sorted into smaller runs to be sorted separately; highly optimized implementations of Quicksort often use insertion or bubble sort to sort these runs once they get small enough.

More sophisticated versions of insertion sort can be smart enough to figure out how much of their data is already in order, and can therefore grow their sorted region by more than one item on each pass; these versions of insertion sort will finish in one pass if the whole array is already sorted.
In the best case of an already sorted array, this implementation of straight insertion sort takes O(n) time: each iteration, the first remaining element of the input is only compared with the last element of the result.
It takes O(n2) time in the average and worst cases, which makes it impractical for sorting large numbers of elements; however, the inner loop is very fast, which often makes it one of the fastest algorithms around for sorting small numbers of elements, typically less than 10 or so.
Quicksort works by dividing the array to be sorted into smaller runs to be sorted separately; highly optimized implementations of Quicksort often use insertion or bubble sort to sort these runs once they get small enough.

Added: 25a47
---

Removed: 27d48


HomePage | Recent Changes | Preferences
Search: