几种排序算法的效率比较
/*
下面的程序是我的上学期数据结构的课程设计
希望对即将学习数据结构的朋友有一点点帮助
因为马上就要离开网络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
L->Length=i;
}
BOOL LT(int i, int j,int* CmpNum)
{
(*CmpNum)++;
if (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
{
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
PivotKey=L->Record[low].num;
while (low < high)
{
while (low
{
high–;
(*CmpNum)++;
}
(*ChgNum)++;
memcpy(&L->Record[low],&L->Record[high],sizeof(Data));
while (low
}
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
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
{
(*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
{
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
{
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
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
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
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
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
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
*/
- 什么是GTK+?
- 快速排序、插入排序和选择排序