# Insertion sort Algorithm, flowchart and C, C++ Code

In this article, we are going to learn about **Insertion Sort, its algorithm, flow chart and C, C++ program to implement Insertion sort**.

Submitted by Raunak Goswami, on August 12, 2018

In the last article, we discussed about the bubble sort with algorithm, flowchart and code. In this article, we are going to discuss about another basic sorting technique i.e. **insertion sort**.

As the name suggests the sorting is done by using successive **insertions** of the key element selected by the sorting algorithm at its correct place. As the sorting begins the key element chosen is always the second element so that there can be at least one element at the left of the key element to be compared. After comparison the key element is swapped with the element at its left if required and the key element is changed to the element at the immediate right of the previous key element after that the key element is again compared with the elements at it left and the element is swapped with the key element after comparison if required, if the sorting is done in ascending order then the key element should always be greater than the element at its left if not, then the swapping is performed with key element and vice versa for the descending order.

Let us look at the **algorithm of insertion sort** for a better understanding of the logic to be used:

Insertion sort(a[],n)for j->2 to n do key <-a[j] i<-j-1 while i>=0 and a[i]>key do a[i+1]<-a[i] i<-i+1 a[i+1]<-key end

The algorithm can also be explained in the form of a flowchart as follows:

### C code for Insertion sort

#include<stdio.h> int main() { int a[6]; int key; int i,j; int temp; printf("Enter any six elements to be sorted using insertion sort\n"); for(i=0;i<6;i++) { scanf("%d",&a[i]); } for(j=1;j<6;j++) { key=a[j]; i=j-1; while((i>=0)&&(a[i]>=key)) { temp=a[i+1]; a[i+1]=a[i]; a[i]=temp; i=i-1; } a[i+1]=key; } printf("elements after sorting using insertion sort are \n"); for(i=0;i<6;i++) { printf("%d \n",a[i]); } return 0; }

**Output**

### C++ code for Insertion sort

#include <iostream.h> using namespace std; int main() { int key,i,j,temp; int c,a[100]; cout<<"enter the no of elements you want to enter"<<endl; cout<<c<<endl; cout<<"Enter the elements"<<endl; for(i=0;i<c;i++) { cin>>a[i]; } for(j=1;j<c;j++) { key=a[j]; i=j-1; while((i>=0)&&(a[i]>=key)) { temp=a[i+1]; a[i+1]=a[i]; a[i]=temp; i=i-1; } a[i+1]=key; } cout<<"elements after sorting using insertion sort are \n"<<endl; for(i=0;i<6;i++) { cout<<" "<<a[i]<<endl; } return 0; }

**Output**

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.