130 likes | 298 Views
Fundamentals of Algorithms MCS - 2 Lecture # 14. Insertion Sort. Insertion Sort. Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time . The main idea of insertion sort is
E N D
Fundamentals of Algorithms MCS - 2 Lecture # 14
Insertion Sort • Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. • The main idea of insertion sort is • Start by considering the first two elements of the array data. If found out of order, swap them • Consider the third element; insert it into the proper position among the first three elements. • Consider the fourth element; insert it into the proper position among the first four elements. • … … • Thus in this algorithm, we keep the left part of the array sorted and take element from the right and insert it in the left part at its proper place. Due to this process of insertion, it is called insertion sort.
The Insertion Sort Algorithm The sorted side starts with just the first element, which is not necessarily the smallest element. The sorted side grows by taking the front element from the unsorted side... ...and inserting it in the place that keeps the sorted side arranged from small to large.
How to Insert One Element Sometimes we are lucky twice in a row. Copy the new element to a separate location.
How to Insert One Element Shift elements in the sorted side, creating an open space for the new element. Continue shifting elements...
How to Insert One Element Continue shifting elements... ...until you reach the location for the new element. Copy the new element back into the array, at the correct location.
How to Insert One Element The last element must also be inserted. Start by copying it... Sorted Result
Pseudo code of Insertion Sort • INSERTION-SORT (A) • n ← length[A] • 1 for i ← 1 to n -1 • 2 do key ← A[i] • 3 Insert A[i] into the sorted sequence A[1 . . i- 1] • 4j← i - 1 • 5 while j >= 0 and A[j] > key • 6 do A[j + 1] ← A[j] • 7j ← j - 1 • 8A[j + 1] ←key
Analysis of Insertion Sort • Insertion sort is one of the fastest algorithms for sorting very small arrays. • The best case input is an array that is already sorted. • In this case insertion sort has a linear running time (i.e., Θ(n)). • During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array. • The worst case input is an array sorted in reverse order. • In this case every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. • For this case insertion sort has a quadratic running time (i.e., O(n2)).
Analysis of Insertion Sort • There are outer and inner loops. Due to these two loops, we can understand that it is also like n2algorithm. • In the sort process, there may be a situation that every iteration inserts an element at the start of the array by shifting all sorted elements along. Now if we have to bring the second element to its position, there will be need of shifting the first element. • This means that we have to shift one element. • Similarly, for placing the third element at the start position (we are discussing the worst case scenario in which at every iteration the element has to go to the first position), we have to shift two elements. • Thus we sum up all the shifting, the total becomes 2 + 3 + 4 +……. + n-1 + n • The summation can be written as follows. • Total = (2 + n ) (n -1) / 2 • = O (n2) • From this expression, we see that when the value of n increases, the value of n2will dominate. It will increase significantly with respect to n. Thus we see that insertion sort is also an n2algorithm like selection sort.