fundamentals of algorithms mcs - 2 lecture # 14

13
Fundamentals of Algorithms MCS - 2 Lecture # 14

Upload: sal

Post on 21-Feb-2016

65 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Fundamentals of Algorithms MCS - 2 Lecture # 14

Fundamentals of Algorithms

MCS - 2

Lecture # 14

Page 2: Fundamentals of Algorithms MCS - 2 Lecture # 14

Insertion Sort

Page 3: 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.

Page 4: Fundamentals of Algorithms MCS - 2 Lecture # 14

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.

Page 5: Fundamentals of Algorithms MCS - 2 Lecture # 14

How to Insert One Element

Sometimes we are lucky twice in a row.

Copy the new element to a separate location.

Page 6: Fundamentals of Algorithms MCS - 2 Lecture # 14

How to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

Continue shifting elements...

Page 7: Fundamentals of Algorithms MCS - 2 Lecture # 14

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

Page 8: Fundamentals of Algorithms MCS - 2 Lecture # 14

The last element must also be inserted. Start by copying it...

Sorted Result

How to Insert One Element

Page 9: Fundamentals of Algorithms MCS - 2 Lecture # 14

Insertion Sort

Page 10: Fundamentals of Algorithms MCS - 2 Lecture # 14

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] 4 j ← i - 1 5 while j >= 0 and A[j] > key 6 do A[j + 1] ← A[j] 7 j ← j - 1 8 A[j + 1] ← key

Page 11: Fundamentals of Algorithms MCS - 2 Lecture # 14

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)).

Page 12: Fundamentals of Algorithms MCS - 2 Lecture # 14

Analysis of Insertion Sort There are outer and inner loops. Due to these two loops, we can understand that it is also

like n2 algorithm. 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 n2 will

dominate. It will increase significantly with respect to n. Thus we see that insertion sort is also an n2 algorithm like selection sort.

Page 13: Fundamentals of Algorithms MCS - 2 Lecture # 14

Good Luck ! ☻