×

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an element and insert it in its appropriate place. Insertion sort is not a fast sorting algorithm because it uses the nested loops to shift the elements in their place. But this is a better sorting technique than bubble sort and selection sort because the complexity of insertion sort is less than both.

If you have n elements in the array, you will need (n-1) pass to sort it.

Complexity table of Insertion sort

ComplexityBest caseAverage caseWorst case
TimeO(n)O(n2)O(n2)
Space  O(1)

Insertion sort algorithm

Insertion_sort(A)
 {   
Repeat for i = 2 to the length of A
      {          
 item = A[i]       
    j = i – 1           
Repeat while (j>0 and A[j] > item)  
               
{                   
 A[j+1] = A[j]                  
  j = j – 1         
        } // end of while loop        
  A[j+1] = item  
    } // end of for loop 
} // end of the insertion loop  

Insertion sort example

Suppose you have the following array, which you have to sort.

1186192

In insertion sort, the first two elements are compared.

There are 6 elements in this array, so you will need 5 iterations to sort it.

Iteration 1: Since 8 is smaller than 11, it will change its place among themselves.       
  8 11 6 1 9 2
Iteration 2: Now, you will compare the next two elements (11 and 6). Since 6 is smaller than 11, it will change its place among themselves. After this, 8 and 6 will compare, 6 is smaller than 8, so it will change its place among themselves.
6 8 11 1 9 2
Iteration 3: Now, you will compare 11 and 1. Since 1 is smaller than 11, it will change its place among themselves. After this, 8 and 1 will compare, 1 is smaller than 8, so it will change its place among themselves.
 1 6 8 11 9 2
Iteration 4: Now, you will compare 11 and 9. Since 9 is smaller than 11, it will change its place among themselves.   
  1 6 8 9 11 2
Iteration 5: Now, you will compare 11 and 2. Since 2 is smaller than 11, it will change its place among themselves. After this, 9 and 2 will compare, 2 is smaller than 9, so it will change its place among themselves.  
1 2 6 8 9 11  

Insertion sort program in C language:

#include<stdio.h>
   void main ()
   {      
int i, j, k, temp;       
int a[6] = { 11, 8, 6, 1, 9, 2};       
 printf("\n Insertion sort  \n");      
 for(k=1; k<6; k++)       
 {         
  temp = a[k];         
  j= k-1;           
while(j>=0 && temp <= a[j])          
 {              
 a[j+1] = a[j];               
 j = j-1;           
}           
a[j+1] = temp;      
 }      
 for(i=0;i<6;i++)      
 {           
printf("\n%d\n",a[i]);  
     } 
  }    

Output

Insertion sort
1 
2 
6 
8 
9 
11

Insertion sort program in java language:

public class InsertionSort
 {   
public static void main(String[] args) 
{       
int[] a = {11, 8, 6, 1, 9, 2};
       
for(int k=1; k<10; k++)
       {         
 int temp = a[k];
          
 int j= k-1;          
while(j>=0 && temp <= a[j])          
 {              
 a[j+1] = a[j];               
 j = j-1;           
}           
a[j+1] = temp;
       }       
System.out.println("Insertion sort ");
       for(int i=0;i<10;i++)
       {         
 System.out.println(a[i]);  
  } 
  } 
  }    

Output

Insertion sort
1 
2 
6 
8 
9 
11

Related Topics

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

B Tree vs B + Tree: Data Structure

Difference Between B Tree and B+ Tree What is B Tree? B-Tree is a self-balancing or special type of m-way tree. B-Trees are used mainly in disc access. If we want...

3 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree,...

4 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.