Insertion Sort


// Insertion Sorting

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,n[10],temp;
 printf("Enter 10 numbers one by one ");
 for(i=0;i<10;i++)
 scanf("%d",&n[i]);
 printf("The original numbers are \n");
 for(i=0;i<10;i++)
 printf("%5d",n[i]);

 for(i=0;i<10;i++)
 {
  temp=n[i];

  for(j=i-1;j>=0&&temp<n[j];j--)
  {
   n[j+1]=n[j];
  }
   n[j+1]=temp;
  }

  printf("\nNumbers are in sorted order\n");
  for(i=0;i<10;i++)
  {
   printf("%d",n[i]);
  }
getch();
}

Post a Comment

Previous Post Next Post