/*
The following program is my Data Structure Course Design for the last semester
I hope this will be helpful to my friends who are about to learn data structures.
Because I am about to leave the network for 2 months, it is a little parting gift.
Liond8 2004-3-20
*/
# Include "stdio. h"
# Include "stdlib. h"
# Include "string. h"
# Include "time. h"
# Include "windows. h"
# Include "winbase. h"
# Define MAXSIZE 1024*5
# Define TRUE 1
# Define FALSE 0
Typedef int BOOL;
Typedef struct StudentData
{
Int num;/* this is a key word */
} Data;
Typedef struct LinkList
{
Int Length;
Data Record [MAXSIZE];
} LinkList;
Int RandArray [MAXSIZE];
/*************** Banner ********************** *********/
Void banner ()
{
Printf ("\ n \ t ***************************** * ************ \ n ");
Printf ("\ t Data Structure Course Design \ n ");
Printf ("\ t \ tMade by LionD8. 2003.6.30 \ n ");
Printf ("\ t \ tPlese press enter. \ n ");
Printf ("\ t ********************************* *********");
Getchar ();
System ("cls.exe ");
}
****************** ******/
Void RandomNum ()
{
Int I;
Srand (int) time (NULL ));
For (I = 0; I <MAXSIZE; I ++)
RandArray [I] = (int) rand ();
Return;
}
/*************************************** ***************/
Void InitLinkList (LinkList * L)
{
Int I;
Memset (L, 0, sizeof (LinkList ));
RandomNum ();
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I];
L-> Length = I;
}
Bool lt (int I, int j, int * CmpNum)
{
(* CmpNum) ++;
If (I <j) return TRUE;
Return FALSE;
}
Void Display (LinkList * L)
{
FILE * f;
Int I;
If (f = fopen ("SortRes.txt", "w") = NULL)
{
Printf ("can't open file \ n ");
Exit (0 );
}
For (I = 0; I <L-> Length; I ++)
Fprintf (f, "% d \ n", L-> Record [I]. num );
Fclose (f );
}
*************/
Void ShellInsert (LinkList * L, int dk, int * CmpNum, int * ChgNum)
{
Int I, j;
Data Temp;
For (I = dk; I <L-> Length; I ++)
{
If (LT (L-> Record [I]. num, L-> Record [I-dk]. num, CmpNum ))
{
Memcpy (& Temp, & L-> Record [I], sizeof (Data ));
For (j = I-dk; j> = 0 & LT (Temp. num, L-> Record [j]. num, CmpNum); j-= dk)
{
(* ChgNum) ++;
Memcpy (& L-> Record [j + dk], & L-> Record [j], sizeof (Data ));
}
Memcpy (& L-> Record [j + dk], & Temp, sizeof (Data ));
}
}
}
Void ShellSort (LinkList * L, int dlta [], int t, int * CmpNum, int * ChgNum)
{
Int k;
For (k = 0; k <t; k ++)
ShellInsert (L, dlta [k], CmpNum, ChgNum );
}
/***************************************/
***********************/
Int Partition (LinkList * L, int low, int high, int * CmpNum, int * ChgNum)
{
Data Temp;
Int foreign tkey;
Memcpy (& Temp, & L-> Record [low], sizeof (Data ));
Repeated tkey = L-> Record [low]. num;
While (low {
While (low {
High --;
(* CmpNum) ++;
}
(* ChgNum) ++;
Memcpy (& L-> Record [low], & L-> Record [high], sizeof (Data ));
While (low {
Low ++;
(* CmpNum) ++;
}
(* ChgNum) ++;
Memcpy (& L-> Record [high], & L-> Record [low], sizeof (Data ));
}
Memcpy (& L-> Record [low], & Temp, sizeof (Data ));
Return low;
}
Void QSort (LinkList * L, int low, int high, int * CmpNum, int * ChgNum)
{
Int optional tloc = 0;
If (low {
Optional tloc = Partition (L, low, high, CmpNum, ChgNum );
QSort (L, low, PivotLoc-1, CmpNum, ChgNum );
QSort (L, repeated tloc + 1, high, CmpNum, ChgNum );
}
}
Void QuickSort (LinkList * L, int * CmpNum, int * ChgNum)
{
QSort (L, 0, L-> Length-1, CmpNum, ChgNum );
}
/*************************************** ******/
************************** **/
Void HeapAdjust (LinkList * L, int s, int m, int * CmpNum, int * ChgNum)
{
Data Temp;
Int j = 0;
S ++;
Memcpy (& Temp, & L-> Record [s-1], sizeof (Data ));
For (j = 2 * s; j <= m; j * = 2)
{
If (j <m & LT (L-> Record [J-1]. num, L-> Record [j]. num, CmpNum) + + j;
If (! LT (Temp. num, L-> Record [J-1]. num, CmpNum) break;
(* ChgNum) ++;
Memcpy (& L-> Record [s-1], & L-> Record [J-1], sizeof (Data ));
S = j;
}
Memcpy (& L-> Record [s-1], & Temp, sizeof (Data ));
}
Void HeapSort (LinkList * L, int * CmpNum, int * ChgNum)
{
Int I = 0;
Data Temp;
For (I = L-> Length/2-1; I> = 0; I --)
HeapAdjust (L, I, L-> Length, CmpNum, ChgNum );
For (I = L-> Length; I> 1; I --)
{
Memcpy (& Temp, & L-> Record [0], sizeof (Data ));
(* ChgNum) ++;
Memcpy (& L-> Record [0], & L-> Record [I-1], sizeof (Data ));
Memcpy (& L-> Record [I-1], & Temp, sizeof (Data ));
HeapAdjust (L, 0, I-1, CmpNum, ChgNum );
}
}
********************* *******/
Void BubbleSort (LinkList * L, int * CmpNum, int * ChgNum)
{
Int I, j;
Data temp;
For (I = 0; I <MAXSIZE-1; I ++)
{
For (j = 0; j <MAXSIZE-i-1; j ++)
{
If (! LT (L-> Record [j]. num, L-> Record [j + 1]. num, CmpNum ))
{
(* ChgNum) ++;
Memcpy (& temp, & L-> Record [j], sizeof (Data ));
Memcpy (& L-> Record [j], & L-> Record [j + 1], sizeof (Data ));
Memcpy (& L-> Record [j + 1], & temp, sizeof (Data ));
}
}
}
}
/*************************************** *******************/
******************* *************/
Int SelectMinKey (LinkList * L, int k, int * CmpNum)
{
Int Min = k;
For (; k <L-> Length; k ++)
{
If (! LT (L-> Record [Min]. num, L-> Record [k]. num, CmpNum ))
Min = k;
}
Return Min;
}
Void SelSort (LinkList * L, int * CmpNum, int * ChgNum)
{
Int I, j;
Data temp;
For (I = 0; I <L-> Length; I ++)
{
J = SelectMinKey (L, I, CmpNum );
If (I! = J)
{
(* ChgNum) ++;
Memcpy (& temp, & L-> Record [I], sizeof (Data ));
Memcpy (& L-> Record [I], & L-> Record [j], sizeof (Data ));
Memcpy (& L-> Record [j], & temp, sizeof (Data ));
}
}
}
/*************************************** ***********************/
Void SelectSort ()
{
Printf ("\ n 0. InsertSort .");
Printf ("\ n 1. ShellSort .");
Printf ("\ n 2. QuickSort .");
Printf ("\ n 3. HeapSort .");
Printf ("\ n 4. BubbleSort .");
Printf ("\ n 5. SelectSort .");
Printf ("\ n 6. AllAbove .");
Printf ("\ n \ t Please Select Num :");
}
/*************************************** *******************/
/*************************************** *******************/
Void allabve (LinkList * L, int * CmpNum, int * ChgNum)
{
Int TempTime, I;
Int SpendTime;
Int dlta [3] = {7, 3, 1 };
Int Indata [1] = {1 };
TempTime = (int) GetTickCount ();
ShellSort (L, Indata, 1, & CmpNum [0], & ChgNum [0]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tInserSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [0], ChgNum [0], SpendTime );
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I]; // random sequence Reset
TempTime = (int) GetTickCount ();
ShellSort (L, dlta, 3, & CmpNum [1], & ChgNum [1]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tShellSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [1], ChgNum [1], SpendTime );
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I]; // random sequence Reset
TempTime = (int) GetTickCount ();
QuickSort (L, & CmpNum [2], & ChgNum [2]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tQuickSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [2], ChgNum [2], SpendTime );
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I]; // random sequence Reset
TempTime = (int) GetTickCount ();
HeapSort (L, & CmpNum [3], & ChgNum [3]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tHeapSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [3], ChgNum [3], SpendTime );
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I]; // random sequence Reset
TempTime = (int) GetTickCount ();
BubbleSort (L, & CmpNum [4], & ChgNum [4]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tBubbleSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [4], ChgNum [4], SpendTime );
For (I = 0; I <MAXSIZE; I ++)
L-> Record [I]. num = RandArray [I]; // random sequence Reset
TempTime = (int) GetTickCount ();
SelSort (L, & CmpNum [5], & ChgNum [5]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ n \ tSelectSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [5], ChgNum [5], SpendTime );
}
Void main ()
{
Int select = 0;
Int dlta [3] = {7, 3, 1 };
Int Indata [1] = {1 };
Int CmpNum [6], ChgNum [6];
Int SpendTime = 0;
Int TempTime;
LinkList L;
InitLinkList (& L );
Memset (CmpNum, 0, sizeof (CmpNum ));
Memset (ChgNum, 0, sizeof (ChgNum ));
Banner ();
SelectSort ();
Scanf ("% d", & select );
Switch (select)
{
Case 0:
TempTime = (int) GetTickCount ();
ShellSort (& L, Indata, 1, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tInserSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 1:
TempTime = (int) GetTickCount ();
ShellSort (& L, dlta, 3, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tShellSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 2:
TempTime = (int) GetTickCount ();
QuickSort (& L, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tQuickSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 3:
TempTime = (int) GetTickCount ();
HeapSort (& L, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tHeapSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 4:
TempTime = (int) GetTickCount ();
BubbleSort (& L, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tBubbleSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 5:
TempTime = (int) GetTickCount ();
SelSort (& L, & CmpNum [select], & ChgNum [select]);
SpendTime = (int) GetTickCount ()-TempTime;
Printf ("\ tSelectSort :");
Printf ("\ n \ tCompare number = % d \ tChange number = % d \ tSepndTime = % dms", CmpNum [select], ChgNum [select], SpendTime );
Break;
Case 6:
Allabve (& L, CmpNum, ChgNum );
Break;
Default:
Printf ("\ n Input error! ");
}
Display (& L );
Printf ("\ n \ tTest over, please press enter! \ N ");
Getchar ();
Getchar ();
}
/*
Test Results
The test results of the six algorithms for sorting 1024x5 random series are as follows:
1. InserSort:
Compare number = 6407568 Change number = 6397342 SepndTime = 1349 ms
2. ShellSort:
Compare number = 1044703 Change number = 1017712 SepndTime = 127 ms
3. QuickSort:
Compare number = 72478 Change number = 30118 SepndTime = 0 ms
4. HeapSort:
Compare number = 110696 Change number = 58691 SepndTime = 18 ms
5. BubbleSort:
Compare number = 13104640 Change number = 6849429 SepndTime = 1992 ms
6. SelectSort:
Compare number = 13109760 Change number = 5111 SepndTime = 1188 ms
*/
High Efficiency: Fast sorting, heap sorting, Hill sorting, insert sorting, select sorting, and Bubble Sorting