Insertion Sort
Insertion sort is a simple sorting algorithm, in which the sorted array (or list) is built and comparison of one entry at a time. It is much less efficient on large lists than more algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
It have very simple implementation and efficient for small data sets
/**************************************************************/ /*PROGRAM TO PERFORM INSERTION SORT IN C LANGUAGE*/ /**************************************************************/
#include<stdio.h> #include<conio.h>
#define INF -999
void main() { int a[10],n,i,j,temp;
clrscr();
a[0]=INF; printf("Enter the size of array: "); scanf("%d",&n); printf("\nEnter the elements of array:\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=2;i<=n;i++) { temp=a[i]; j=i-1; while(temp<a[j]) { a[j+1]=a[j]; j--; } a[j+1]=temp; }
printf("\nAfter sorting the elements of array are:\n"); for(i=1;i<=n;i++) { printf("%d ",a[i]); } getch(); }
http://
http://
Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com
Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=18
Click here to go on website
|