Insertion
Sort
Insertion Sorting
•It is a simple Sorting algorithm which sorts the array by shifting
elements
One by one.
Following are some of the important characteristics of Insertion
Sort.
It has one of the simplest implementation
It is efficient for smaller data sets, but very inefficient for larger
lists.
Insertion Sort is adaptive, that means it reduces its total
number of steps if given a partially sorted list, hence it
increases its efficiency.
It is better than Selection Sort and Bubble Sort algorithms.
Its space complexity is less, like Bubble Sorting, insertion
sort also requires a single additional memoryspace.
It is Stable, as it does not change the relative order of elements
with
equal keys
Insertion Sorting
Insertion Sort
Example :
9 2 7 5 1 4 3 6
Insertion Sort
Sorted
section
9 2 7 5 1 4 3 6
We start by dividing the array in a sorted section and an
unsorted section. We put the first element as the only element in
the sorted section, and the rest of the array is the unsorted
section.
Insertion Sort
Sorted Item to
position
section
9 2 7 5 1 4 3 6
The first element in the unsorted section is the next element
to be put into the correct position.
Insertion Sort
Item
to
positi
on
9 2 7 5 1 4 3 6
We copy the element to be placed into another variable so it doesn’t get
overwritten.
Insertion Sort
9 2 7 5 1 4 3 6
2
compare
If the previous position is more than the item being placed, copy
the value into the next position
Insertion Sort
belongs here
9 9 7 5 1 4 3 6
If there are no more items in the sorted section to compare with, the
item to be placed must go at the front.
Insertion Sort
2 9 7 5 1 4 3 6
Insertion Sort
2 9 7 5 1 4 3 6
Insertion Sort
Item to
position
2 9 7 5 1 4 3 6
Insertion Sort
2 9 7 5 1 4 3 6
7
compare
Insertion Sort
Copied from
previous
position
belongs here
2 9 9 5 1 4 3 6
7
compare
If the item in the sorted section is less than the item to place, the
item to place goes after it in the array.
Insertion Sort
2 7 9 5 1 4 3 6
Insertion Sort
2 7 9 5 1 4 3 6
Insertion Sort
Item to
position
2 7 9 5 1 4 3 6
Insertion Sort
2 7 9 5 1 4 3 6
5
compare
Insertion Sort
2 7 9 9 1 4 3 6
5
compare
Insertion Sort
belongs here
2 7 7 9 1 4 3 6
5
compare
Insertion Sort
2 5 7 9 1 4 3 6
Insertion Sort
2 5 7 9 1 4 3 6
Insertion Sort
Item to
position
2 5 7 9 1 4 3 6
Insertion Sort
2 5 7 9 1 4 3 6
1
compare
Insertion Sort
2 5 7 9 9 4 3 6
1
compare
Insertion Sort
2 5 7 7 9 4 3 6
1
compare
Insertion Sort
2 5 5 7 9 4 3 6
1
compare
Insertion Sort
belongs here
2 2 5 7 9 4 3 6
1
Insertion Sort
1 2 5 7 9 4 3 6
Insertion Sort
1 2 5 7 9 4 3 6
Insertion Sort
Item to
position
1 2 5 7 9 4 3 6
Insertion Sort
1 2 5 7 9 4 3 6
4
compare
Insertion Sort
1 2 5 7 9 9 3 6
4
compare
Insertion Sort
1 2 5 7 7 9 3 6
4
compare
Insertion Sort
belongs here
1 2 5 5 7 9 3 6
4
compare
Insertion Sort
1 2 4 5 7 9 3 6
Insertion Sort
1 2 4 5 7 9 3 6
Insertion Sort
Item to
position
1 2 4 5 7 9 3 6
Insertion Sort
1 2 4 5 7 9 3 6
3
compare
Insertion Sort
1 2 4 5 7 9 9 6
3
compare
Insertion Sort
1 2 4 5 7 7 9 6
3
compare
Insertion Sort
1 2 4 5 5 7 9 6
3
compare
Insertion Sort
belongs here
1 2 4 4 5 7 9 6
3
compare
Insertion Sort
1 2 3 4 5 7 9 6
Insertion Sort
1 2 3 4 5 7 9 6
Insertion Sort
Item to
position
1 2 3 4 5 7 9 6
Insertion Sort
1 2 3 4 5 7 9 6
6
compare
Insertion Sort
1 2 3 4 5 7 9 9
6
compare
Insertion Sort
belongs here
1 2 3 4 5 7 7 9
6
compare
Insertion Sort
1 2 3 4 5 6 7 9
Insertion Sort
1 2 3 4 5 6 7 9
SORTED!
Pseudo Code
• for i = 0 to n – 1
j =1
while j > 0 and A[j] < A[j – 1]
swap(A[j], A[j-1])
j =j - 1
CODE OF INSERTION SORT
void insertion_sort (int arr[], int length)
{
int j,temp;
for (int i = 0; i < length; i++)
{
j = i;
while (j > 0 && arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
}
Best Times to Use Insertion Sort
• When the data sets are relatively small.
Moderately efficient
• When you want a quick easy
implementation.
Not hard to code Insertion sort.
• When data sets are mostly sorted already.
(1,2,4,6,3,2)
Worst Times to Use Insertion Sort
• When the data sets are relatively large.
– Because the running time is quadratic
• When data sets are completely unsorted
– Absolute worst case would be reverse
ordered. (9,8,7,6,5,4)
• Advantages
– Good running time for “almost sorted” arrays
(n)
– Simple implementation.
– Stable, i.e. does not change the relative order
of elements with equal keys
Disadvantages
It is less efficient on list containing more number of
elements.
As the number of elements increases the performance
of the program would be slow.
Insertion sort needs a large number of element shifts.
N^2/2 comparisons in worst and average case
Complexity
• Worst case: we have to do n comparisons for 1st
iteration, n-1 for next and n-2 for next until we
reach at 1.
Time complexity= O(n2)
• Best Case: when the list is already sorted
Time Complexity= O(n)
• Average Case:
Time Complexity=O(n2)