Today we will learn the Insertion Sort Program in C. So before starting you should have a little bit knowledge about insertion sorting.

## What is Insertion Sort?

The basic idea of this method is to place an unsorted element into its correct position in a growing sorted list of elements. We select one element from the unsorted set at a time and insert it into its correct position in the sorted set.

The Process continues till the unsorted set becomes empty.

**For Example:**

In order to arrange playing cards, we pick one card at a time and insert this card in its proper position in the cards held in the hand.

**Illustration:**

Given Data is: **15 7 22 3 14 2**

when a new element is to be inserted into the sorted list, it is compared with the elements in the sorted list and inserted at its correct place.

This is done by shifting elements which are greater than the element one position to the right. The comparison is done backwards from the last element.

For example, to insert 14 in the example above, it is first compared with 22. Since 22>14, 22 is shifted right. Then it is compared with 15. Since 15>14, 15 is shifted right. Then it is compared with 7, Since 7<14, 14 is placed to the right of 7.

The Insertion Process begins from the second element since the first element can be directly placed in the sorted set at position 0.

## Insertion Sort Algorithm:

Let x is an array of n elements. x[0] may be considered as a sorted array of one element. The unsorted list is x[1] to x[n-1].

**1st Step:** START.

**2nd Step:** j=1 i.e unsorted position starts from 1.

**3rd Step:** key=x[j].

**4th Step:** i=j-1 i.e sorted set starts from j-1.

**5th Step:** while x[i]>key and i>=0 then x[i+1]=x[i]. i.e shift element to the right.

**6th Step:** x[i+1]=key i.e insert key in correct position.

**7th Step:** j=j+1 i.e next unsorted element.

**8th Step:** If j<n go to step 3.

**9th Step:** STOP.

## Efficiency of Insertion Sort:

**Best Case:** If the initial data is sorted, only one comparison is made in each pass i.e each element will be compared only with the last element in the sorted list. So the best case time complexity is O(n).

**Example:** If the elements are 2,3,7,14,15,22 then 3 is compared only with 2, 7 with 3, 14 with 7 and so on.

**Worst case:** If the initial data is in the reverse order each element will be compared with each element in the sorted list.

**Example:** If the elements are 22,15,14,7,3,2 then 15 is compared with 22, 14 is compared with 22 and 15,7 is compared with 22,15 and 14, 3 is compared with 22,15,14 and 7 and so on.

The total number of comparisons for the n-1 passes will be 1+2+…+(n-1) = n(n-1) / 2 which is **O(n^2)**.

### Advantages:

It is a simple sorting method.

It is in-place sorting method.

It’s a Stable sorting method.

Best case complexity is O(n).

Most suitable sorting method if the elements are almost sorted.

### Disadvantages:

Worst Case efficiency is O(n^2).

## 1. Insertion Sort Program in C

#include <stdio.h> int main() { int n, i, j, temp; int arr[64]; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 1 ; i <= n - 1; i++) { j = i; while ( j > 0 && arr[j-1] > arr[j]) { temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; j--; } } printf("Sorted list in ascending order:\n"); for (i = 0; i <= n - 1; i++) { printf("\t%d", arr[i]); } return 0; }

**Output:**

In the above program, the compiler asks the user to enter the n elements and stored in the array arr. Then we are comparing those elements so they can be arranged properly using insertion sort. And then print on the screen.

**Run time Test Cases:**

**1st Case: Average Case-**

In this case, elements are entered in random order.

**2nd Case: Best Case-**

Here the elements are already sorted.

**3rd Case: Worst Case-**

In this case, the elements are in the reverse order.

## 2. Insertion Sort Program in C Using While Loop

In this program, we will use while loop instead of for loop. Don’t get afraid the logic behind the program is the same.

#include <stdio.h> int main() { int a[100], number, i, j, temp; printf("\n Please Enter the total Number of Elements : "); scanf("%d", &number); printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++) scanf("%d", &a[i]); i = 1; while( i <= number - 1) { j = i; while( j > 0 && a[j - 1] > a[j]) { temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; j--; } i++; } printf("\n Insertion Sort Result : "); for(i = 0; i < number; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }

**Output:**

## 3. Insertion Sort Using Functions

In this program, we will use the user-declared function to sort the elements in the order.

#include <stdio.h> void insertionSort(int a[], int number) { int i, j, temp; for(i = 1; i <= number - 1; i++) { for(j = i; j > 0 && a[j - 1] > a[j]; j--) { temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; } } } int main() { int a[100], number, i; printf("\n Please Enter the total Number of Elements : "); scanf("%d", &number); printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++) scanf("%d", &a[i]); insertionSort(a, number); printf("\n Insertion Sort Result : "); for(i = 0; i < number; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }

**Output:**

**Also Read:**

**C Program to Make Simple Calculator****Armstrong Number Program in C****Fibonacci Series Program in C****Decimal to Binary Conversion Program in C****Reverse a String in C****Program to Reverse a Number in C****Hello World Program in C****Palindrome Program in C****Leap Year Program in C****Factorial Program in C****Prime Number Program in C****Program to print prime numbers from 1 to n****Anagram program in C.****Calculate LCM Program in C.****Addition of Two Numbers in C****Matrix Multiplication Program in C.****Playfair Cipher Program in C.****GCD Program in C.****Tower of Hanoi in C.****First Fit Program in C****Swapping of two numbers in C****Menu Driven Program in C.****Odd or Even number program in C****Round Robin Program in C****Simple Interest Program in C****Quadratic Equation Program in C.****Bubble Sort Program in C.****Merge Sort Program in C.**

## Leave A Comment