C Program to implement Insertion sort


Levels of difficulty: / perform operation:

Insertion sorting technique is the elementary sorting technique. Insertion sort sorts one element at a time, It is just like manual sorting by humans. Insertion sort is better for small set of elements. Insertion sort is slower than heap sort, shell sort, quick sort,and merge sort.

C Program

#include<stdio.h>
#include<conio.h>
void inst_sort(int []);
void main() {
	int num[5],count;
	clrscr();
	printf("\nEnter the Five Elements to sort:\n");
	for (count=0;count<5;count++)
	  scanf("%d",&num[count]);
	inst_sort(num);
	printf("\n\nElements after sorting: \n");
	for (count=0;count<5;count++)
	  printf("%d\n",num[count]);
	getch();
}
// Function for Insertion Sorting
void inst_sort(int num[]) {
	int i,j,k;
	for (j=1;j<5;j++) {
		k=num[j];
		for (i=j-1;i>=0 && k<num[i];i--)
		   num[i+1]=num[i];
		num[i+1]=k;
	}
}