0% found this document useful (0 votes)
3 views5 pages

Insertion Sort Algorithm

The document provides an overview of the Insertion Sort Algorithm, explaining its process of sorting an unsorted list by iteratively inserting elements into their correct positions. It outlines the advantages, such as simplicity and efficiency for small or nearly sorted lists, as well as disadvantages like inefficiency for large lists. Additionally, it discusses applications of Insertion Sort in various sorting algorithms and scenarios.

Uploaded by

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

Insertion Sort Algorithm

The document provides an overview of the Insertion Sort Algorithm, explaining its process of sorting an unsorted list by iteratively inserting elements into their correct positions. It outlines the advantages, such as simplicity and efficiency for small or nearly sorted lists, as well as disadvantages like inefficiency for large lists. Additionally, it discusses applications of Insertion Sort in various sorting algorithms and scenarios.

Uploaded by

elaineila1309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ALDERSGATE COLLEGE INC.

College of Arts, Sciences, Education & Information Technology


Information Technology Program

Course : Data Structures and Algorithm


Module Number :
Module Title : Insertion Sort Algorithm
Pre-requisite Skills :
Level :
Instructor :
Allotted Time :

Insertion Sort Algorithm

Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of
an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing
cards in your hands. You split the cards into two groups: the sorted cards and the unsorted
cards. Then, you pick a card from the unsorted group and put it in the right place in the sorted
group.
 We start with the second element of the array as the first element is assumed to be
sorted.
 Compare the second element with the first element if the second element is smaller then
swap them.
 Move to the third element, compare it with the first two elements, and put it in its correct
position
 Repeat until the entire array is sorted.
Output
5 6 11 12 13

arr = {23, 1, 10, 5, 2}


Initial:
 Current element is 23
 The first element in the array is assumed to be sorted.
 The sorted part until 0th index is : [23]
First Pass:
 Compare 1 with 23 (current element with the sorted part).
 Since 1 is smaller, insert 1 before 23 .
 The sorted part until 1st index is: [1, 23]
Second Pass:
 Compare 10 with 1 and 23 (current element with the sorted part).
 Since 10 is greater than 1 and smaller than 23 , insert 10 between 1 and 23 .
 The sorted part until 2nd index is: [1, 10, 23]
Third Pass:
 Compare 5 with 1 , 10 , and 23 (current element with the sorted part).
 Since 5 is greater than 1 and smaller than 10 , insert 5 between 1 and 10
 The sorted part until 3rd index is : [1, 5, 10, 23]
Fourth Pass:
 Compare 2 with 1, 5, 10 , and 23 (current element with the sorted part).
 Since 2 is greater than 1 and smaller than 5 insert 2 between 1 and 5 .
 The sorted part until 4th index is: [1, 2, 5, 10, 23]
Final Array:
 The sorted array is: [1, 2, 5, 10, 23]

Advantages and Disadvantages of Insertion Sort


Advantages
 Simple and easy to implement.
 Stable sorting algorithm.
 Efficient for small lists and nearly sorted lists.
 Space-efficient as it is an in-place algorithm.
 Adoptive. the number of inversions is directly proportional to number of swaps. For
example, no swapping happens for a sorted array and it takes O(n) time only.
Disadvantages
 Inefficient for large lists.
 Not as efficient as other sorting algorithms (e.g., merge sort, quick sort) for most cases.
Applications of Insertion Sort
Insertion sort is commonly used in situations where:
 The list is small or nearly sorted.
 Simplicity and stability are important.
 Used as a subroutine in Bucket Sort
 Can be useful when array is already almost sorted (very few inversions)
 Since Insertion sort is suitable for small sized arrays, it is used in Hybrid Sorting
algorithms along with other efficient algorithms like Quick Sort and Merge Sort. When
the subarray size becomes small, we switch to insertion sort in these recursive
algorithms. For example IntroSort and TimSort use insertions sort.
[Link]

You might also like