Expt 13 DSA Lab (ECS) Implement Insertion Sort Algorithm
Expt 13 DSA Lab (ECS) Implement Insertion Sort Algorithm
_____________________________________________________________________________________
Experiment No. 13
Title Implement insertion sort algorithm
Name
Year/Branch
Roll No.
UIN
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Theory:
Sorting is one of the most important operations performed by computers. It is used to arrange the data in
ascending and descending order.
Types of sorting techniques
Bubble sort
Insertion sort
Selection sort
Shell sort
Quick sort
Merge sort
Radix sort
Heap sort
Insertion sort: An insertion sort is one that sorts a set of records by inserting records into an existing sorted
file. In insertion sort, the list is divided into two parts sorted and unsorted. In each pass, the first element of
the unsorted sublist has to find its appropriate place and then it has to be inserted there. Hence the name,
insertion sort. Following are some of the important characteristics of Insertion Sort. 1. It has one of the
simplest implementation 2. It is efficient for smaller data sets, but very inefficient for larger lists. 3. Insertion
Sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it
increases its efficiency. 4. It is better than Selection Sort and Bubble Sort algorithms. 5. It is a Stable sorting,
as it does not change the relative order of elements with equal keys
● Step 1 - Assume that first element in the list is in sorted portion and all the remaining elements are in
unsorted portion.
● Step 2: Take first element from the unsorted portion and insert that element into the sorted portion in
the order specified.
● Step 3: Repeat the above process until all the elements from the unsorted portion are moved into the
sorted portion.
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Algorithm:
Algorithm:
InsertionSort (x, n)
temp ← A[i]
A[j+1] ← A[j]
A[j+1] ←temp
To sort an unsorted list with 'n' number of elements, we need to make (1+2+3+......+n-1) = (n (n-1))/2
number of comparisions in the worst case. If the list is already sorted then it requires 'n' number of
comparisions.
Worst Case : O(n2)
Best Case : Ω(n)
Average Case : Θ(n2)
Program:
#include<stdio.h>
#include<conio.h>
void main(){
j = j - 1;
}
arr[j + 1] = temp;
}
Output:
Conclusion: Insertion sort is simple to implement and works well for small amount of data. The array is
searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array).