Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts

Thursday, 21 March 2013

insertion sort

file open is used.you can change it accordingly.assuming you know little bit of c coding.



#include<stdio.h>
#include<conio.h>
#include<time.h>
int main()
{
FILE *f;
f=fopen("INPUT.TXT","r");

    int i,a[10],n;
   
    printf("Enter no of Elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
                  scanf("%d",&a[i]);
   
    int j,temp;
    time_t stime=time(NULL);
    for(i=1;i<=n-1;i++)
    {
     for(j=0;j<i;j++)
     {
      if(a[i]>a[j])
      {
       a[i]=(a[i]+a[j])-(a[j]=a[i]);
      }
     }
    }
    time_t ftime=time(NULL);
    printf("\nSorted time:%ld\n",difftime(ftime,stime));
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
    getch();
}