0% found this document useful (0 votes)
23 views11 pages

Fainal Project

The document is a presentation on insertion sort. It begins with introductions and then discusses what insertion sort is, how it works, provides an example, and shows pseudocode for the algorithm. Insertion sort is an in-place comparison sorting algorithm that maintains a sorted sub-list by taking elements and inserting them into the appropriate place in the sorted portion.

Uploaded by

md anik hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Fainal Project

The document is a presentation on insertion sort. It begins with introductions and then discusses what insertion sort is, how it works, provides an example, and shows pseudocode for the algorithm. Insertion sort is an in-place comparison sorting algorithm that maintains a sorted sub-list by taking elements and inserting them into the appropriate place in the sorted portion.

Uploaded by

md anik hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Welcome to my

presentation

Name: Md. Anik Hasan


ID: 201-35-572 Instructor
Section: PC-A Name: Shariful Islam
Department of Software
Department of Software
Engineering
engineering
Topics : Insertion Sort
What is Insertion Sort

 This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained


which is always sorted. For example, the lower part of an array is maintained to be
sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its
appropriate place and then it has to be inserted there. Hence the name, insertion
sort.
How Insertion Sort Works?
Consider the following elements are to be sorted in ascending order-
6, 2, 11, 7, 5

Insertion sort works as-

Firstly,
• It selects the second element (2).
• It checks whether it is smaller than any of the elements before it.
• Since 2 < 6, so it shifts 6 towards right and places 2 before it.
• The resulting list is 2, 6, 11, 7, 5.

Secondly,
• It selects the third element (11).
• It checks whether it is smaller than any of the elements before it.
• Since 11 > (2, 6), so no shifting takes place.
• The resulting list remains the same.
Thirdly,

It selects the fourth element (7).


It checks whether it is smaller than any of the elements before it.
Since 7 < 11, so it shifts 11 towards right and places 7 before it.
The resulting list is 2, 6, 7, 11, 5.

Fourthly,

It selects the fifth element (5).


It checks whether it is smaller than any of the elements before it.
Since 5 < (6, 7, 11), so it shifts (6, 7, 11) towards right and places 5 before
them.
The resulting list is 2, 5, 6, 7, 11.

As a result, sorted elements in ascending order are-

2, 5, 6, 7, 11
Insertion Sort Example-

 Consider the following elements are to be sorted in ascending order-


 6, 2, 11, 7, 5

 The above insertion sort algorithm works as illustrated below-


 Step-01: For i = 1
Step-02: For i = 2

Step-03: For i = 3
Step-04: For i = 4

Loop gets terminated as ‘i’ becomes 5. The state of array after the loops are finished-

With each loop cycle,


•One element is placed at the correct location in the sorted sub-array until array A is completely sorted.
Code implementation

 for (i = 1 ; i < n ; i++)


 {
 key = A [ i ];
 j = i - 1;
 while(j > 0 && A [ j ] > key)
 {
 A [ j+1 ] = A [ j ];
 j--;
 }
 A [ j+1 ] = key;
 }

 Here,
 i = variable to traverse the array A
 key = variable to store the new number to be inserted into the sorted sub-array
 j = variable to traverse the sorted sub-array
Important Notes-

 Insertion sort is not a very efficient algorithm when data sets are large.
 This is indicated by the average and worst case complexities.
 Insertion sort is adaptive and number of comparisons are less if array is partially
sorted.

You might also like