The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Insertion Sort

Insertion sort is a very playful sorting of algorithm in which the sorted array or list is built one element at a time. Sorting in insertion sort algorithm is very much likely to arranging the deck of cards while playing bridge.

Advantages - Insertion Sort

  • Insertion sort algorithm requires less memory space.
  • Insertion sort algorithm is easy to implement and efficient to use on small amount of data.
  • Insertion sort algorithm is more efficient when few or more elements are already sorted.

Disadvantages - Insertion Sort

  • Insertion sort algorithm has a linear running time of 0(n). This is because for unsorted list a element needs to sort for every iteration.
  • Straight forward, insertion sort algorithm will have worst case for descending order.

C program - Insertion Sort

Here is the program to demonstrate Insertion Sort.

insertion-sort.c
#include <stdio.h>
void insertion_sort(int arry[], int n);
int main()
{
int arry[30], i, n;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
printf("\n Enter %d elements in the array : \n",n);
for(i=0; i<n; i++)
scanf("%d",&arry[i]);
insertion_sort(arry,n);
printf("\n Sorted Array in ascending order is : \n");
for(i=0; i<n; i++)
printf("%d ", arry[i]);
return 0;
}
void insertion_sort(int arry[], int n)
{
int i, j, temp;
for(i = 1; i<n; i++)
{
temp = arry[i];
j = i - 1;
while((temp < arry[j]) && (j >= 0))
{
arry[j+1] = arry[j];
j--;
}
arry[j+1] = temp;
}
}
Enter the size of the array : 5 
Enter 5 elements in the array :
4 3 1 2 5 
Sorted Array in ascending order is : 
1 2 3 4 5

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you