分享
 
 
 

几种排序算法的效率比较

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

文章作者:LionD8

文章出处:虎盟网络安全小组(rohu.com)

/*

下面的程序是我的上学期数据结构的课程设计

希望对即将学习数据结构的朋友有一点点帮助

因为马上就要离开网络2个月了,算是一点点临别的礼物

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\n\t\t******************************************\n");

printf("\t\t 数据结构课程设计\n");

printf("\t\tMade by LionD8. 2003.6.30\n");

printf("\t\tPlese press enter.\n");

printf("\t\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 PivotKey;

memcpy(&Temp,&L->Record[low],sizeof(Data));

PivotKey=L->Record[low].num;

while (low < high)

{

while (low<high && L->Record[high].num >= PivotKey)

{

high--;

(*CmpNum)++;

}

(*ChgNum)++;

memcpy(&L->Record[low],&L->Record[high],sizeof(Data));

while (low<high && L->Record[low].num <= PivotKey)

{

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 PivotLoc=0;

if (low < high)

{

PivotLoc=Partition(L,low,high,CmpNum,ChgNum);

QSort(L,low,PivotLoc-1,CmpNum,ChgNum);

QSort(L,PivotLoc+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\t\t\t Please Select Num:");

}

/**********************************************************/

/**********************************************************/

void AllAbove(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]; //随机数列复位

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]; //随机数列复位

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]; //随机数列复位

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]; //随机数列复位

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]; //随机数列复位

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\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\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\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\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\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\n\tCompare number=%d\tChange number=%d\tSepndTime=%dms",CmpNum[select],ChgNum[select],SpendTime);

break;

case 6:

AllAbove(&L,CmpNum,ChgNum);

break;

default:

printf("\n Input error !");

}

Display(&L);

printf("\n\n\tTest over, please press enter!\n");

getchar();

getchar();

}

/*

测试结果

对1024×5大小的随机数列排序6种算法的测试结果分别如下:

1.InserSort:

Compare number=6407568 Change number=6397342 SepndTime=1349ms

2. ShellSort:

Compare number=1044703 Change number=1017712 SepndTime=127ms

3. QuickSort:

Compare number=72478 Change number=30118 SepndTime=0ms

4. HeapSort:

Compare number=110696 Change number=58691 SepndTime=18ms

5. BubbleSort:

Compare number=13104640 Change number=6849429 SepndTime=1992ms

6. SelectSort:

Compare number=13109760 Change number=5111 SepndTime=1188ms

*/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有