/*Selection sorting selects the initially the first element as minimum valued and compare it with the all elements*/
***************************************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define FNAME1 "best case"
#define FNAME2 "avg case"
#define FNAME3 "wrost case"
FILE *f1,*f2,*f3;
void main()
{
int i,j,ch,n,k,min,minpos,a[10000];
int count=0;
clrscr();
printf("1-> Best case\t2-> Average case\t 3-> Worst case\n\n");
printf("Enter your choice:");
scanf("%d",&ch);
printf("Enter the size of elements of file\t:");
scanf("%d",&n);
switch(ch)
{
case 1:
f1=fopen(FNAME1,"w");
for(i=0;i<n;i++)
{
a[i]=i;
printf("%d\t",a[i]);
fprintf(f1,"%d\n",a[i]);
}
fclose(f1);
f1=fopen(FNAME1,"r");
i=0;
while(!feof(f1))
{
fscanf(f1,"%d\n",&a[i]);
i++;
}
fclose(f1);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
break;
case 2:
f2=fopen(FNAME2,"w");
for(i=0;i<n;i++)
{
k=rand();
fprintf(f2,"%d\t",k);
}
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);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
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);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
break;
}
getch();
}
****************************************************************************
***************************************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define FNAME1 "best case"
#define FNAME2 "avg case"
#define FNAME3 "wrost case"
FILE *f1,*f2,*f3;
void main()
{
int i,j,ch,n,k,min,minpos,a[10000];
int count=0;
clrscr();
printf("1-> Best case\t2-> Average case\t 3-> Worst case\n\n");
printf("Enter your choice:");
scanf("%d",&ch);
printf("Enter the size of elements of file\t:");
scanf("%d",&n);
switch(ch)
{
case 1:
f1=fopen(FNAME1,"w");
for(i=0;i<n;i++)
{
a[i]=i;
printf("%d\t",a[i]);
fprintf(f1,"%d\n",a[i]);
}
fclose(f1);
f1=fopen(FNAME1,"r");
i=0;
while(!feof(f1))
{
fscanf(f1,"%d\n",&a[i]);
i++;
}
fclose(f1);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
break;
case 2:
f2=fopen(FNAME2,"w");
for(i=0;i<n;i++)
{
k=rand();
fprintf(f2,"%d\t",k);
}
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);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
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);
for(i=0;i<n;i++) //sorting loop
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
count++;
min=a[j];
minpos=j;
}
}
a[minpos]=a[i];
a[i]=min;
} //sorting loop ends
printf("After Sorting... \n");
for(i=0;i<n;i++)
{
fscanf(f1,"%d\n",&a[i]);
printf("%d\t",a[i]);
}
printf("The total counts required:%d",count);
break;
}
getch();
}
****************************************************************************
No comments:
Post a Comment