Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

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



ADVERTISEMENT
ADVERTISEMENT