Sunday, 17 June 2012

Insertion sort


#include<stdio.h>
#include<conio.h>
int i;
void insert(int b[20],int m)
{
 int j,p,temp;
 for(p=1;p<m;p++)
 {
  temp=b[p];
  for(j=p;j>0 && b[j-1]>temp;j--)
   b[j]=b[j-1];
  b[j]=temp;
 }
}
void main()
{
 int a[20],n;
 clrscr();
 printf("\n Enter size of array ");
 scanf("%d",&n);
 printf("\n Enter the elements:");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 insert(a,n);
 printf("\n Elements after sorting :");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);
 getch();
}

No comments:

Post a Comment