Functional requirements are as follows:
Sorting algorithm comparison: Shellsort, Quicksort, Heapsort, mergesort algorithm implementation,
The sort time comparison for the same dataset.
Source:
# include <stdio.h>
# include <time.h>
# define MAXSIZE 2000
typedef struct{
int key[maxsize];
int length;
}list;
long int compcount;
long int shiftcount;
void menu (int *m)/*retun m*/
{
int i;
Char menu[6][15]={"1 CREATE", "2 IMPORT", "3 SORT", "4 show result",
"5 SAVE result", "6 EXIT"};
CLRSCR ();
printf ("SORT COMPARE system\n");
for (i=0;i<6;i++) printf ("%s\n", Menu[i]);
printf ("\ n Please select (1-6): \ n");
scanf ("%d", m);
}
void Menusort (int *m)/*retun m*/
{
int i;
Char menusort[5][15]={"1 SHELL Sort", "2 QUICK Sort", "3 HEAP sort",
"4 MERGE Sort", "5 All Sort"};
CLRSCR ();
printf ("sort\n");
for (i=0;i<5;i++) printf ("%s\n", Menusort[i]);
printf ("\ n Please select (1-5): \ n");
scanf ("%d", m);
}
void menushow (int *m)/*retun m*/
{
int i;
Char menushow[4][20]={"1 SHELL sort Result" and "2 QUICK SORT result",
' 3 HEAP sort result ', ' 4 MERGE sort result '};
CLRSCR ();
printf ("Show SORT result\n");
for (i=0;i<4;i++) printf ("%s\n", Menushow[i]);
printf ("\ n Please select (1-4): \ n");
scanf ("%d", m);
}
void Menusave (int *m)
{
int i;
Char menusave[4][20]={"1 SHELL sort Result" and "2 QUICK SORT result",
' 3 HEAP sort result ', ' 4 MERGE sort result '};
CLRSCR ();
printf ("save:\n");
for (i=0;i<4;i++) printf ("%s\n", Menusave[i]);
printf ("\ n Please select (1-4): \ n");
scanf ("%d", m);
}
void Create (List *l)
{
int i;
printf ("How MANY data?\n");
scanf ("%d",& ((*l). length));
For (i=1;i<= (*l). length;i++)
{
printf ("\nplease INPUT the%dth data:\n", i);
scanf ("%d",& (*l). Key[i]);
}
printf ("\ncreate COMPLETE!\n");
}
int Listopen (list *l,char *filename)
{
int k=1;
FILE *data;
Data=null;
Data=fopen (filename, "RB");
while (! feof (data))
{
FSCANF (data, "%d",& (*l). Key[k]);
k++;
}
(*l). length=k-1;
}
void import (list *l)/*fix l*/
{
Char filename[255];
int i;
printf ("\nplease INPUT the FILE PATH and name:\n");
scanf ("%s", filename);
CLRSCR ();
Listopen (L,filename);
For (i=1;i< (*l). length;i++) printf ("%d", (*l). Key[i]);
printf ("\npress AnyKey return to mainmenu...\n");
Getch ();
}
void Save (List L)
{
FILE *data;
Char filename[255];
int R;
printf ("\nplease INPUT the FILE PATH and name:\n");
scanf ("%s", filename);
Data=fopen (filename, "WB");
for (r=1;r<=l.length;r++) fprintf (data, "%d\n", L.key[r]);
fclose (data);
printf ("SAVE ok! \ n Press any KEY to return the MAINMENU ... ");
Getch ();
}
List Shellsort (list L)/*retun l_shell*/
{
int i,j,gap,x,n;
compcount=shiftcount=0;
N=l.length;
GAP=N/2;
while (gap>0)
{
compcount++;
for (i=gap+1;i<=n;i++)
{
compcount++;
J=i-gap;
while (j>0)
{
compcount++;
if (L.key[j]>l.key[j+gap])
{
compcount++;
x=l.key[j];shiftcount++;
l.key[j]=l.key[j+gap];shiftcount++;
l.key[j+gap]=x;shiftcount++;
J=j-gap;
}
else j=0;
}
}
GAP=GAP/2;
}
return L;
}
void Shell (list l,list *ls,float *timeshell)/*return Ls,timeshell.
Must add an "Getch"!! */
{
clock_t Start,end;
Start=clock ();
(*ls) =shellsort (L);
End=clock ();
*timeshell= (End-start)/clk_tck;
printf ("\nshellsort Cost time:%f SECONDS.", *timeshell);
printf ("Compare%d times. Shfit%d times.\n ", compcount,shiftcount);
}
int Partition (list * Pl,int Low,int High)
{
int PivotKey;
pl->key[0]=pl->key[low];shiftcount++;
pivotkey=pl->key[low];shiftcount++;
while (Low{
compcount++;
while (Low{compcount++;compcount++;--high;}
pl->key[low]=pl->key[high];shiftcount++;
while (Low{compcount++;compcount++; ++low;}
pl->key[high]=pl->key[low];shiftcount++;
}
pl->key[low]=pl->key[0];shiftcount++;
return to Low;
}/*partition*/
void Qsort (list * Pl,int Low,int High)
{
int pivotloc;
if (Low{
compcount++;
Pivotloc=partition (Pl,low,high);
Qsort (pl,low,pivotloc-1);
Qsort (Pl,pivotloc+1,high);
}
}/*qsort*/
List QuickSort (list PL)
{
compcount=shiftcount=0;
Qsort (&pl,1,pl.length);
return PL;
}/*quicksort*/
void Quick (list l,list *lq,float *timequick)/*must add an "Getch"!! */
{
clock_t Start,end;
Start=clock ();
(*LQ) =quicksort (L);
End=clock ();
*timequick= (End-start)/clk_tck;
printf ("\nquicksort Cost time:%f SECONDS.", *timequick);
printf ("Compare%d times. Shfit%d times.\n ", compcount,shiftcount);
}
void Sift (list l,int l,int m)
{
int i,j,x;
I=l;
J=2*i;
X=l.key[i];
while (J<=M)
{
compcount++;
if (j<m && l.key[j]<l.key[j+1]) {j++;compcount++;compcount++;}
if (X<l.key[j])
{
compcount++;
l.key[i]=l.key[j];shiftcount++;
i=j;shiftcount++;
J=2*i;
}
else j=m+1;
}
l.key[i]=x;shiftcount++;
}
List heapsort (list L)
{
int i,w;
compcount=shiftcount=0;
for (i=l.length/2;i>=1;i--) {sift (l,i,l.length); compcount++;}
for (i=l.length;i>=2;i--)
{
compcount++;
w=l.key[i];shiftcount++;
l.key[i]=l.key[1];shiftcount++;
l.key[1]=w;shiftcount++;
Sift (l,i-1,1);
}
return L;
}
void Heap (list l,list *lh,float *timeheap)
{
clock_t Start,end;
Start=clock ();
(*LH) =heapsort (L);
End=clock ();
*timeheap= (End-start)/clk_tck;
printf ("\nheapsort Cost time:%f SECONDS.", *timeheap);
printf ("Compare%d times. Shfit%d times.\n ", compcount,shiftcount);
}
void Merge (int source[],int result[],int size,int N)
{
int lb1,lb2,ub1,ub2,p,i,j;
lb1=0;
p = =;
while ((lb1+size) <n)
{
compcount++;
Lb2=lb1+size;
ub1=lb2-1;
if ((lb2+size-1) >n)
{ub2=n-1; compcount++; shiftcount++;}
Else
{ub2=lb2+size-1; compcount++; shiftcount++;}
I=LB1;
J=LB2;
while ((I<=UB1) && (J<=UB2))
{
compcount++;compcount++;
if (Source[i]<=source[j])
{result[p++]=source[i++]; shiftcount++; compcount++;}
Else
{result[p++]=source[j++]; shiftcount++; compcount++;}
}
while (I<=UB1)
{result[p++]=source[i++]; shiftcount++; compcount++;}
while (J<=UB2)
{result[p++]=source[j++]; shiftcount++; compcount++;}
lb1=ub2+1;
}
I=LB1;
while (P<n)
{compcount++; result[p++]=source[i++];shiftcount++;}
}
void MergeSort (List *l)
{
int n= (*l). length;
int S=1;
int *temp= (int *) malloc (n*sizeof (int));
compcount=shiftcount=0;
if (temp==null)
{
printf ("Out of Memory");
Return
}
while (S<n)
{
compcount++;
Merge ((*l). Key,temp,s,n);
s*=2;
Merge (temp, (*l). Key,s,n);
s*=2;
}
compcount++;
}
void Domerge (list l,list *lm,float *timemerge)/*must add an "Getch"!! */
{
clock_t Start,end;
Start=clock ();
MergeSort (&L);
End=clock ();
(*LM) =l;
*timemerge= (End-start)/clk_tck;
printf ("\nmergesort Cost time:%f SECONDS.", *timemerge);
printf ("Compare%d times. Shfit%d times.\n ", compcount,shiftcount);
}
Main ()
{
List l,ls,lq,lh,lm;
int lock3=0,lock4=0,lock5=0,run=1,lock41=0,lock42=0,lock43=0,lock44=0;
int comd,r;
float Timeshell,timequick,timeheap,timemerge;
while (run==1)
{
Start
menu (&COMD);
Switch (COMD)
{
Case 1:
Create (&L);
Lock3=1;
Break
Case 2:
Import (&L);
Lock3=1;
Goto start;
Case 3:
if (lock3==0) goto start;
Menusort (&COMD);
Lock4=1;
Lock5=1;
Switch (COMD)
{
Case 1:
Lock41=1;
Shell (L,&ls,×hell);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 2:
Lock42=1;
Quick (L,&lq,&timequick);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 3:
Lock43=1;
Heap (L,&LH,&TIMEHEAP);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 4:
Lock44=1;
Domerge (L,&lm,&timemerge);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 5:
Lock41=1;
Lock42=1;
Lock43=1;
Lock44=1;
Shell (L,&ls,×hell);
Quick (L,&lq,&timequick);
Heap (L,&LH,&TIMEHEAP);
Domerge (L,&lm,&timemerge);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 6:
Goto start;
}
Case 4:
if (lock4==0) goto start;
Menushow (&COMD);
Switch (COMD)
{
Case 1:
if (lock41==0) goto start;
for (r=1;r<=ls.length;r++)
printf ("%d", ls.key[r]);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 2:
if (lock42==0) goto start;
for (r=1;r<=lq.length;r++)
printf ("%d", lq.key[r]);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 3:
if (lock43==0) goto start;
for (r=1;r<=lh.length;r++)
printf ("%d", lh.key[r]);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 4:
if (lock44==0) goto start;
for (r=1;r<=lm.length;r++)
printf ("%d", lm.key[r]);
printf ("\ n Press any KEY to return MAIN menu ... \ n");
Getch ();
Goto start;
Case 6:
Goto start;
}
Case 5:
if (lock5==0) goto start;
Menusave (&COMD);
Switch (COMD)
{
Case 1:
if (lock41==0) goto start;
Save (LS);
Break
Case 2:
if (lock42==0) goto start;
Save (LQ);
Break
Case 3:
if (lock43==0) goto start;
Save (LH);
Break
Case 4:
if (lock44==0) goto start;
Save (LM);
Break
Case 6:
Break
}
Break
Case 6:
Exit (0);
}
}
}