Program for all types of Sortings in C.

#include <stdio.h>
#include<math.h>
int SIZE=10;
int ncomps[7],nswaps[7],time[7];
int fl=0;
FILE *fpp,*fp;
void insertion();
void exchange();
void shell1();
void shell2();
void fileopen();
void shell3();
void bubble();
void heap();
int sh2();
int sh3();
float value();
main()
{
int i;
fpp=fopen("as.txt","w");
insertion();
exchange();
shell1();
shell2();
shell3();
bubble();
heap();
fclose(fpp);
for (i=0;i<7;i++)
{
printf ("no %d----%d----%d-----%d\n",i+1,ncomps[i],nswaps[i],time[i]);
}
}

void insertion()
{
int aj1=0,aj2=0;
int i,j,k,a[SIZE];
fileopen();
for (i=0;i<SIZE;i++){
fscanf(fp,"%d",&a[i]);
}

printf("1-insertion sort\n");
for (i=1;i<SIZE;i++)
{
j=a[i];
for (k=i-1;k>=0&&a[k]>j;k--)
{a[k+1]=a[k];aj1++;}
a[k+1]=j;
aj2++;
}

ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj1;
fl++;
for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);
return;
}

void exchange()
{
fileopen();
int aj1=0,aj2=0,aj3=0;
int i,t,mid,j,a[SIZE];
for (i=0;i<SIZE;i++){
fscanf(fp,"%d",&a[i]);
}

printf("2- exchange sort\n");
for (i=0;i<SIZE-1;i++)
{
t=i;
for(j=i+1;j<SIZE;j++)
{aj3++;
if(a[t]>a[j])
{t=j;aj1++;}}
mid=a[t];
a[t]=a[i];
a[i]=mid;
aj2++;
}
ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj3;
fl++;
for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);
return;
}

void shell1()
{
int aj1=0,aj2=0,aj3=0;
int q,v,i,j,a[SIZE],mid,qe;
fileopen();
for (i=0;i<SIZE;i++){
fscanf(fp,"%d",&a[i]);
}
printf("3- shell sort\n");
for (i=(SIZE/2);i>0;(i/=2))
for (j=i;j<SIZE;j++)
{
mid=a[j];
for (q = j; q >= i && mid < a[q- i];q = q - i)
{a[q] = a[q-i];aj1++;}
a[q] = mid;aj2++;
}
ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj1;
fl++;
for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);
return;
}

void shell2()
{
printf("4- shell sort hibbard's increament \n");
int i,v,q,j,mid,k,a[SIZE];
fileopen();
for (i=0;i<SIZE;i++){
fscanf(fp,"%d",&a[i]);
}
int aj1=0,aj2=0,aj3=0;

for(k=sh2(),i=(pow(2,k)-1);k!=0;k--,i=pow(2,k)-1)
for (j=i;j<SIZE;j++)
{
mid=a[j];
for (q = j; q >= i && mid < a[q- i];q = q - i)
{a[q] = a[q-i];aj1++;}
a[q] = mid;aj2++;
}

ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj1;
fl++;

for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);
return;
}

void shell3()
{
printf("5- shell sort sedgewick increament\n");
int i,j,q,k,a[SIZE],mid;
float r;
fileopen();
for (i=0;i<SIZE;i++)
{
fscanf(fp,"%d",&a[i]);
}

int aj1=0,aj2=0,aj3=0;

for(k=sh3(),i=(int)(((9*pow(4,k))-(9*pow(2,k))+1));k>=0;k--,i=(int)(((9*pow(4,k))-(9*pow(2,k))+1)))
for (j=i;j<SIZE;j++)
{
mid=a[j];

for (q = j; q >= i && mid < a[q- i];q = q - i)
{a[q] = a[q-i];aj1++;}
a[q] = mid;aj2++;
}

for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);

ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj1;
fl++;
return;
}
void bubble()
{
printf("6- bubble sort\n");
int i,j,mid,a[SIZE];
int aj1=0,aj2=0,aj3=0;
fileopen();
for (i=0;i<SIZE;i++){
fscanf(fp,"%d",&a[i]);
}

for(i=SIZE-1;i>0;i--)
for(j=0;j<i;j++)
{if(a[j]>a[j+1])
{
mid=a[j];a[j]=a[j+1];a[j+1]=mid;aj2++;
}aj1++;
}
ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj1;
fl++;
for (i=0;i<SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
fclose(fp);
return;
}

int sh2()
{
return (int)(log(SIZE)/log(2));
}

int sh3()
{
float x;
int y1,z1,q1,y2,z2,q2;
x=sqrt(9-(4*(1-SIZE)));

y1=(int)((9+3*x)/2);
z1=(int)((9-3*x)/2);
if(y1>0)
q1=(int)((log(y1))/(log(2)));
else if(z1>0)
q1= (int)((log(z1))/(log(2)));
else q1= 1;
return(q1);
}

void heap()
{
printf("7- heap sort\n");
int k,q,i,j,mid,temp,a[SIZE+1];
fileopen();
a[0]=-1;
int aj1=0,aj2=0,aj3=0;
for (j=1;j<=SIZE;j++)
{
i=j;
fscanf(fp,"%d",&temp);
q=(i/2);
for(a[j]=temp;a[i]>a[q]&&q!=0;i/=2,q/=2)
{aj3++;
mid =a[i];a[i]=a[q];a[q]=mid;
}
}

for (j=SIZE;j!=1;j--)
{
temp=a[j];a[j]=a[1];a[1]=temp;
for (k=1,q=2;(q<j)||(q+1<j);)
{aj1++;aj3++;
if(q+1<j)
{
if(a[q]<a[q+1])
{
mid=a[k];a[k]=a[q+1];a[q+1]=mid;aj2++;
k=k*2+1;
}
else
{
mid=a[k];a[k]=a[q];a[q]=mid;
k*=2;aj2++;
}
}
else if(q<j+1)
{
mid=a[k];a[k]=a[q];a[q]=mid;
k=k*2;aj2++;
}
else break;
q=2*k;
}
}

ncomps[fl]=aj1;nswaps[fl]=aj2;time[fl]=aj3;

for (i=1;i<=SIZE;i++)
fprintf(fpp,"%d\t",a[i]);
fprintf(fpp,"\n");
return;
}

void fileopen()
{
fp=fopen("x.txt","r");
if(fp==NULL)
{
printf("ERROR IN OPEANING THE FILE\n");
exit(1);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s