Wednesday, August 7, 2013

Insertion sort


Program      :  Implement sorting methods using functions Insertion sort
Created By :  JABIR PATHAN

#include<stdio.h>
#include<conio.h>
void main()
 {
    int i,j,arr[30],n,temp,ch,k,d;
    clrscr();
    printf("\n\nHow many elements you want to enter in array==>");
    scanf("%d",&n);
    printf("\n\nEnter the elements==>\n");
    for(i=0;i<n;i++)
          scanf("%d",&arr[i]);
    printf("\naccepted elements are==>\n");
    for(i=0;i<n;i++)
         printf("\narr[%d]=%d",i,arr[i]);
    for(i=1;i<n;i++)
    {
      temp=arr[i];
      for(j=i-1;j>=0&&arr[j]>temp;j--)
       {
        arr[j+1]=arr[j];
        arr[j]=temp;  
      }
     printf("\n\npasses=%d",i);
     for(k=0;k<n;k++)
      {
       printf("\t%d",arr[k]);
      }
    }
   printf("\n\nsorted Elements Are==>\n ");
   for(i=0;i<n;i++)
   {
     printf("\narr[%d]=%d",i,arr[i]);
   }
  getch();
}
Click here to Download this Zip file
Password - JP

No comments:

Post a Comment