Insertion Sort Algorithm
The insertion sort algorithm sorts a list by repeatedly inserting an unsorted element into the correct position in a sorted sublist. The algorithm maintains two sublists in a given array:
- A sorted sublist. This sublist initially contains a single element (an array of one element is always sorted).
- A sublist of elements to be inserted one at a time into the sorted sublist.
Pseudocode
procedure insertion_sort(array : list of sortable items, n : length of list)
i ← 1
while i < n
j ← i
while j > 0 and array[j - 1] > array[j]
swap array[j - 1] and array[j]
j ← j - 1
end while
i ← i + 1
end while
end procedure
Optimizing Insertion Sort
Performing a full swap
procedure insertion_sort(array : list of sortable items, n : length of list)
i ← 1
while i < n
temp ← array[i]
j ← i
while j > 0 and array[j - 1] > temp
array[j] ← array[j - 1]
j ← j - 1
end while
array[j] ← temp
i ← i + 1
end while
end procedure
Example
The following example illustrates how an array changes after each pass through the outer loop of the insertion sort algorithm.
|
Original Array
|
23
|
12
|
18
|
42
|
29
|
37
|
15
|
10
|
|
|
Before 1st Pass
|
23
|
12
|
18
|
42
|
29
|
37
|
15
|
10
|
|
|
After 1st Pass
|
12
|
23
|
18
|
42
|
29
|
37
|
15
|
10
|
|
|
After 2nd Pass
|
12
|
18
|
23
|
42
|
29
|
37
|
15
|
10
|
|
|
After 3rd Pass
|
12
|
18
|
23
|
42
|
29
|
37
|
15
|
10
|
|
|
After 4th Pass
|
12
|
18
|
23
|
29
|
42
|
37
|
15
|
10
|
|
|
After 5th Pass
|
12
|
18
|
23
|
29
|
37
|
42
|
15
|
10
|
|
|
After 6th Pass
|
12
|
15
|
18
|
23
|
29
|
37
|
42
|
10
|
|
|
After 7th Pass
|
10
|
12
|
15
|
18
|
23
|
29
|
37
|
42
|
|
|
Sorted Array
|
10
|
12
|
15
|
18
|
23
|
29
|
37
|
42
|
|
Complexity
Time Complexity: O(n2)
Space Complexity: O(1)
The primary advantage of insertion sort over selection sort is that selection sort must always scan all remaining unsorted elements to find the minimum element in the unsorted portion of the list, while insertion sort requires only a single comparison when the element to be inserted is greater than the last element of the sorted sublist. When this is frequently true (such as if the input list is already sorted or partially sorted), insertion sort is considerably more efficient than selection sort. The best case input is a list that is already correctly sorted. In this case, insertion sort has O(n) complexity.