Efficiency Comparison of several sort Algorithms

Source: Internet
Author: User
Author: liond8
Source: humeng Network Security Group (rohu.com)
 
/*
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 & L-> record [High]. Num> = duplicate tkey)
{
High --;
(* Cmpnum) ++;
}
(* Chgnum) ++;
Memcpy (& L-> record [low], & L-> record [High], sizeof (data ));
While (low <High & L-> record [low]. Num <= duplicate tkey)
{
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. allabve .");
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
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.