Search This Blog

Friday, September 9, 2011

Insertion Sort Program..!!

/*This Program performs the Insertion Sort Algorithm using FILE Structer*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#define FNAME1 "best case"
#define FNAME2 "avg case"
#define FNAME3 "worst case"


FILE *f1,*f2,*f3;  //file pointer
void main()
{
 int i,j,ch,n,k,w,a[10000],c,temp;
 int cnt=0;
 clock_t start,end;
 clrscr();
 printf("1-> Best case\n2-> Average case\n 3-> Worst case\n\n");
 printf("Enter chice:");
 scanf("%d",&ch);
  printf("\nEnter size of elements in file\t:");
  scanf("%d",&n);
 switch(ch)
 {
  case 1:
   f1=fopen(FNAME1,"w");//here file will be created in TC, juss check it..!!
   for(i=0;i<n;i++)
   {
    a[i]=i;
    printf("%d ",a[i]);
    fprintf(f1,"%d\n",a[i]);
   }
   fclose(f1);

   i=0;
   f1=fopen(FNAME1,"r");
   while(!feof(f1))
   {
    fscanf(f1,"%d\n",&a[i]);
    printf("%d",a[i]);
    i++;
   }
   fclose(f1);


  start=clock();
   for(i=1;i<n;i++)    //sorting loop
   {
       for(j=i+1;j>0;j--)
       {
    if(a[j]<a[j-1])
    {
      temp=a[j];
      a[j]=a[j-1];
      a[j-1]=temp;
      cnt++;
    }

       }
   }
   end=clock();
   printf("\nAfter sorting: \n");
   f1=fopen(FNAME1,"w");
   for(i=0;i<n;i++)
   {
    fscanf(f1,"%d\n",&a[i]);
    printf("%d\t ",a[i]);
   }
   fclose(f1);
   printf("\nThe Time taken=%f",(end-start)/CLK_TCK);
   printf("\nThe total counts required= %d",cnt);
  break;
  case 2:
   f2=fopen(FNAME2,"w");
   i=0;
   for(i=0;i<n;i++)
   {

    k=rand();
    fprintf(f2,"%d\t",k);
        // printf("%d",a[i]);
   }
   fclose(f2);
   f2=fopen(FNAME2,"r");
   i=0;
   while(!feof(f2))
   {
    fscanf(f2,"%d\t",&a[i]);
    printf("%d\t",a[i]);
    i++;
   }
   fclose(f2);

   start=clock();
   cnt=0;
   for(i=1;i<n;i++)    //sorting loop
   {
       for(j=i+1;j>0;j--)
       {
    if(a[j]<a[j-1])
    {
      temp=a[j];
      a[j]=a[j-1];
      a[j-1]=temp;
      cnt++;
    }

       }
   }
   end=clock();

   printf("\nAfter sorting: \n");
   f2=fopen(FNAME2,"w");
   for(i=0;i<n;i++)
   {
    fscanf(f2,"%d\n",&a[i]);
    printf("\t%d ",a[i]);
   }
   fclose(f2);
   printf("\nThe TIME taken=%f",(end-start)/CLK_TCK);
   printf("\nThe total counts required= %d",cnt);
  break;


  case 3:
   f3=fopen(FNAME3,"w");
   i=n-1;
   j=0 ;
   while(i>=0)
   {
    a[j]=i;
    fprintf(f3,"%d\n",a[i]);
    printf("%d\t",a[j]);
    i--;

    j++;
   }
   fclose(f3);

   f3=fopen(FNAME3,"r");
   i=0;
   while(!feof(f3))
   {
    fscanf(f3,"%d\t",&a[i]);
    printf("%d\t",a[i]);
    i++;
   }
   fclose(f3);


   start=clock();
   cnt=0;
   for(i=1;i<n;i++)    //sorting loop
   {
       for(j=i+1;j>0;j--)
       {
    if(a[j]<a[j-1])
    {
      temp=a[j];
      a[j]=a[j-1];
      a[j-1]=temp;
      cnt++;
    }

       }
    }
    end=clock();

   printf("\nAfter sorting:");
   f3=fopen(FNAME3,"w");
   for(i=0;i<n;i++)
   {
    fscanf(f3,"%d\n",&a[i]);
    printf("%d ",a[i]);
   }
   fclose(f3);
   printf("\nThe TIME taken=%f",(end-start)/CLK_TCK);
   printf("\nThe total counts required= %d",cnt);
  break;

 }
 getch();
}

/*Enjoy FrenZZ*/

No comments:

Post a Comment