《内部排序算法比较》

《内部排序算法比较》

一、【问题描述】
在教科书中,各种内部排序算法的时间复杂度分析结果只给出算法的大致执行时间。试通过随机数据比较各算法的关键字比较次数和关键字移动次数,以获得直观感受
【基本要求】
(1) 对6种常用内部排序算法进行比较:冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、堆排序
(2) 待排序的表长不小于100,其中数据要用伪随机数产生,至少用5组不同的输入数据做比较
(3) 比较指标为关键字参加的比较次数和关键字的移动次数

二、概要设计
1(可排序表的抽象数据类型定义:
ADT OrderableList{
数据对象:D={ai|ai?IntegerSet,i=1,2,„,n,n?0}
数据关系:R1={|ai-1,ai?D,i=2,„,n}
基本操作:
InitList(n)
操作结果:构造一个长度为n,元素值依次为1,2,„,n的有序表。
RandomizeList(d,isInverseOrder)
操作结果:首先根据isInverseOrder为True或False,将表置为逆序或正序,然后将表进行d(0
?d?8)级随机打乱。d为0时表不打乱,d越大,打乱程度越高。
RecallList()
操作结果:恢复最后一次用RandomizeList随机打乱得到的可排序表。
ListLength()
操作结果:返回可排序表的长度。
ListEmpty()
操作结果:若可排序表为空表,则返回Ture,否则返回False。
BubbleSort( &c, &s)
操作结果:进行起泡排序,返回关键字比较次数c和移动次数s。
InsertSort( &c, &s)
操作结果:进行插入排序,返回关键字比较次数c和移动次数s。
SelectSort ( &c, &s)
操作结果:进行选择排序,返回关键字比较次数c和移动次数s。
QuickSort(&c, &s)
操作结果:进行快速排序,返回关键字比较次数c和移动次数s。
ShellSort(long &c, long &s)
操作结果:进行希尔排序,返回关键字比较次数c和移动次数s。
HeapSort (&c, &s)
操作结果:进行堆排序,返回关键字比较次数c和移动次数s。
ListTraverse(visit())
操作结果:依次对L中的每个元素调用函数visit()。 }ADT OrderableList
2(本程序包含两个模块:
1)主程序模块
void main(){
初始化;
do{
接受命令;
处理命令;
}while(“命令”~=“退出”);
}
2)可排序表单元模块——实现可排序表的抽象数据类型; 各模块之间的调用关系如下:
主程序模块
可排序表单元模块
三、详细设计
1(根据题目要求和可排序表的基本操作特点,可排序表采用整数顺序表存储结构。

  //可排序表的元素类型 
    #define MAXSIZE 10000 //用作示例的顺序表的最大长度 typedef int BOOL; 
    typedef struct{ 
    int key; //关键字项 
    } RedType; //记录类型 
    typedef struct LinkList{ 
    RedType r[MAXSIZE]; 
    int Length; //顺序表长度 } LinkList; 
    int RandArray[MAXSIZE]; //内部操作 
    void RandomNum(){ 
    int i; 
    srand(20000); 
    for (i = 0; i < MAXSIZE; i++) 
    RandArray[i] = (int)rand(); //构建随机序列 } 
    void InitLinkList(LinkList *L){ //建立表 
    int i; 
    memset(L, 0, sizeof(LinkList)); 
    RandomNum(); 
    for (i = 0; i < MAXSIZE; i++) 
    L->r[i].key = RandArray[i]; //赋值 
    L->Length = i; 
    } 
    bool LT(int i, int j, int *CmpNum){ //比较i与j大小,返回0或1 
    (*CmpNum)++; 
    if (i < j) 
    return TRUE; 
    else 
    return FALSE; 
    } 
    void Display(LinkList *L){ //存储表到SortRes.txt文件中 
    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->r[i].key); 
    fclose(f); 
    } 
    //部分操作的伪码算法 
    //希尔排序 
    void ShellInsert(LinkList *L, int dk, int *CmpNum, int *ChgNum){ 
    int i, j; 
    RedType Temp; 
    for (i = dk; i < L->Length; i++){ 
    if (LT(L->r[i].key, L->r[i - dk].key, CmpNum)){ 
    memcpy(&Temp, &L->r[i], sizeof(RedType)); 
    for (j = i - dk; j >= 0 && LT(Temp.key, L->r[j].key, CmpNum); j -= dk){ 
    (*ChgNum)++; 
    memcpy(&L->r[j + dk], &L->r[j], sizeof(RedType)); 
    } 
    memcpy(&L->r[j + dk], &Temp, sizeof(RedType)); 
    } 
    } 
    } 
    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){ 
    RedType Temp; 
    int PivotKey; 
    memcpy(&Temp, &L->r[low], sizeof(RedType)); 
    PivotKey = L->r[low].key; 
    while (low < high){ 
    while (low < high && L->r[high].key >= PivotKey){ 
    high--; 
    (*CmpNum)++; 
    } 
    (*ChgNum)++; 
    memcpy(&L->r[low], &L->r[high], sizeof(RedType)); 
    while (low < high && L->r[low].key <= PivotKey){ 
    low++; 
    (*CmpNum)++; 
    } 
    (*ChgNum)++; 
    memcpy(&L->r[high], &L->r[low], sizeof(RedType)); 
    } 
    memcpy(&L->r[low], &Temp, sizeof(RedType)); 
    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){ 
    RedType Temp; 
    int j = 0; 
    s++; 
    memcpy(&Temp, &L->r[s - 1], sizeof(RedType)); 
    for (j = 2 * s; j <= m; j *= 2){ 
    if (j < m && LT(L->r[j - 1].key, L->r[j].key, CmpNum)) 
    ++j; 
    if (!LT(Temp.key, L->r[j - 1].key, CmpNum)) 
    break; 
    (*ChgNum)++; 
    memcpy(&L->r[s - 1], &L->r[j - 1], sizeof(RedType)); 
    s = j; 
    } 
    memcpy(&L->r[s - 1], &Temp, sizeof(RedType)); } 
    void HeapSort(LinkList *L, int *CmpNum, int *ChgNum){ 
    int i = 0; 
    RedType 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->r[0], sizeof(RedType)); 
    (*ChgNum)++; 
    memcpy(&L->r[0], &L->r[i - 1], sizeof(RedType)); 
    memcpy(&L->r[i - 1], &Temp, sizeof(RedType)); 
    HeapAdjust(L, 0, i - 1, CmpNum, ChgNum); 
    } 
    } 
    //冒泡排序 
    void BubbleSort(LinkList *L, int *CmpNum, int *ChgNum){ 
    int i, j; 
    RedType temp; 
    for (i = 1; i <= MAXSIZE; i++){ 
    for (j = 1; j <= MAXSIZE - i; j++){ 
    if (!LT(L->r[j].key, L->r[j + 1].key, CmpNum)){ 
    (*ChgNum)++; 
    memcpy(&temp, &L->r[j], sizeof(RedType)); 
    memcpy(&L->r[j], &L->r[j + 1], sizeof(RedType)); 
    memcpy(&L->r[j + 1], &temp, sizeof(RedType)); 
    } 
    } 
    } 
    } 
    //选择排序 
    int SelectMinKey(LinkList *L, int k, int *CmpNum){ 
    int Min = k; 
    for (; k < L->Length; k++){ 
    if (!LT(L->r[Min].key, L->r[k].key, CmpNum)) 
    Min = k; 
    } 
    return Min; 
    } 
    void SelSort(LinkList *L, int *CmpNum, int *ChgNum){ 
    int i, j; 
    RedType temp; 
    for (i = 0; i < L->Length; i++){ 
    j = SelectMinKey(L, i, CmpNum); 
    if (i != j){ 
    (*ChgNum)++; 
    memcpy(&temp, &L->r[i], sizeof(RedType)); 
    memcpy(&L->r[i], &L->r[j], sizeof(RedType)); 
    memcpy(&L->r[j], &temp, sizeof(RedType)); 
    } 
    } 
    } 

四:调试分析:
《内部排序算法比较》_第1张图片
《内部排序算法比较》_第2张图片
《内部排序算法比较》_第3张图片
《内部排序算法比较》_第4张图片
《内部排序算法比较》_第5张图片
《内部排序算法比较》_第6张图片
《内部排序算法比较》_第7张图片
《内部排序算法比较》_第8张图片
五、代码清单:

内部排序算法比较.cpp 
//主程序 
#include  
#include  
#include  
#include  
#include  
#include  
#define MAXSIZE 5000 
#define TRUE 1 
#define FALSE 0 
typedef int BOOL; 
typedef struct{ 
int key; 
} RedType; 
typedef struct LinkList{ 
RedType r[MAXSIZE+1]; 
int Length; 
} LinkList; 
int RandArray[MAXSIZE+1]; 
void RandomNum(){ 
int i; 
srand(2000); 
for (i = 1; i <= MAXSIZE; i++) 
RandArray[i] = (int)rand(); } 
void InitLinkList(LinkList *L){ 
int i; 
memset(L, 0, sizeof(LinkList)); 
RandomNum(); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; 
L->Length = i; 
} 
bool LT(int i, int j, int *CmpNum){ 
(*CmpNum)++; 
if (i < j) 
return TRUE; 
else 
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->r[i].key); 
fclose(f); 
} 
//希尔排序 
void ShellInsert(LinkList *L, int dk, int *CmpNum, int *ChgNum){ 
int i, j; 
RedType Temp; 
for (i = dk; i < L->Length; i++){ 
if (LT(L->r[i].key, L->r[i - dk].key, CmpNum)){ 
memcpy(&Temp, &L->r[i], sizeof(RedType)); 
for (j = i - dk; j >= 0 && LT(Temp.key, L->r[j].key, CmpNum); j -= dk){ 
(*ChgNum)++; 
memcpy(&L->r[j + dk], &L->r[j], sizeof(RedType)); 
} 
memcpy(&L->r[j + dk], &Temp, sizeof(RedType)); 
} 
} 
} 
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){ 
RedType Temp; 
int PivotKey; 
memcpy(&Temp, &L->r[low], sizeof(RedType)); 
PivotKey = L->r[low].key; 
while (low < high){ 
while (low < high && L->r[high].key >= PivotKey){ 
high--; 
(*CmpNum)++; 
} 
(*ChgNum)++; 
memcpy(&L->r[low], &L->r[high], sizeof(RedType)); 
while (low < high && L->r[low].key <= PivotKey){ 
low++; 
(*CmpNum)++; 
} 
(*ChgNum)++; 
memcpy(&L->r[high], &L->r[low], sizeof(RedType)); 
} 
memcpy(&L->r[low], &Temp, sizeof(RedType)); 
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){ 
RedType Temp; 
int j = 0; 
s++; 
memcpy(&Temp, &L->r[s - 1], sizeof(RedType)); 
for (j = 2 * s; j <= m; j *= 2){ 
if (j < m && LT(L->r[j - 1].key, L->r[j].key, CmpNum)) 
++j; 
if (!LT(Temp.key, L->r[j - 1].key, CmpNum)) 
break; 
(*ChgNum)++; 
memcpy(&L->r[s - 1], &L->r[j - 1], sizeof(RedType)); 
s = j; 
} 
memcpy(&L->r[s - 1], &Temp, sizeof(RedType)); } 
void HeapSort(LinkList *L, int *CmpNum, int *ChgNum){ 
int i = 0; 
RedType 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->r[0], sizeof(RedType)); 
(*ChgNum)++; 
memcpy(&L->r[0], &L->r[i - 1], sizeof(RedType)); 
memcpy(&L->r[i - 1], &Temp, sizeof(RedType)); 
HeapAdjust(L, 0, i - 1, CmpNum, ChgNum); 
} 
} 
//冒泡排序 
void BubbleSort(LinkList *L, int *CmpNum, int *ChgNum){ 
int i, j; 
RedType temp; 
for (i = 1; i <= MAXSIZE; i++){ 
for (j = 1; j <= MAXSIZE - i; j++){ 
if (!LT(L->r[j].key, L->r[j + 1].key, CmpNum)){ 
(*ChgNum)++; 
memcpy(&temp, &L->r[j], sizeof(RedType)); 
memcpy(&L->r[j], &L->r[j + 1], sizeof(RedType)); 
memcpy(&L->r[j + 1], &temp, sizeof(RedType)); 
} 
} 
} 
} 
//选择排序 
int SelectMinKey(LinkList *L, int k, int *CmpNum){ 
int Min = k; 
for (; k < L->Length; k++){ 
if (!LT(L->r[Min].key, L->r[k].key, CmpNum)) 
Min = k; 
} 
return Min; 
} 
void SelSort(LinkList *L, int *CmpNum, int *ChgNum){ 
int i, j; 
RedType temp; 
for (i = 0; i < L->Length; i++){ 
j = SelectMinKey(L, i, CmpNum); 
if (i != j){ 
(*ChgNum)++; 
memcpy(&temp, &L->r[i], sizeof(RedType)); 
memcpy(&L->r[i], &L->r[j], sizeof(RedType)); 
memcpy(&L->r[j], &temp, sizeof(RedType)); 
} 
} 
} 
void SelectSort(){ 
printf("1. 插入排序\n"); 
printf("2. 希尔排序\n"); 
printf("3. 快速排序\n"); 
printf("4. 堆排序\n"); 
printf("5. 冒泡排序\n"); 
printf("6. 选择排序\n"); 
printf("7. 以上所有排序方式\n"); 
printf("8. 退出程序\n\n"); 
printf("Please Select the Operate:"); } 
void AllAbove(LinkList *L, int *CmpNum, int *ChgNum){ 
int TempTime, i,j; 
int SpendTime; 
int dlta[3] = { 
7, 3, 1 
}; 
int Indata[1] = { 
1 
}; 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n插入排序:\n"); 
TempTime = (int)GetTickCount(); 
ShellSort(L, Indata, 1, &CmpNum[0], &ChgNum[0]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[0], 
ChgNum[0],SpendTime); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n希尔排序:\n"); 
TempTime = (int)GetTickCount(); 
ShellSort(L, dlta, 3, &CmpNum[1], &ChgNum[1]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[1], 
ChgNum[1],SpendTime); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n快速排序:\n"); 
TempTime = (int)GetTickCount(); 
QuickSort(L, &CmpNum[2], &ChgNum[2]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[2], 
ChgNum[2],SpendTime); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n堆排序:\n"); 
TempTime = (int)GetTickCount(); 
HeapSort(L, &CmpNum[3], &ChgNum[3]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[3], 
ChgNum[3],SpendTime); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n冒泡排序:\n"); 
TempTime = (int)GetTickCount(); 
BubbleSort(L, &CmpNum[4], &ChgNum[4]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[4], 
ChgNum[4],SpendTime); 
for (i = 1; i <= MAXSIZE; i++) 
L->r[i].key = RandArray[i]; //随机数列复位 
printf("\n选择排序:\n"); 
TempTime = (int)GetTickCount(); 
SelSort(L, &CmpNum[5], &ChgNum[5]); 
SpendTime = (int)GetTickCount() - TempTime; 
printf("\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", CmpNum[5], 
ChgNum[5],SpendTime); 
} 
void main(){ 
int i,j; 
int select = 0; 
int dlta[3] = {7, 3, 1}; 
int Indata[1] = {1}; 
int CmpNum[8], ChgNum[8]; 
int SpendTime = 0; 
int TempTime; 
LinkList L; 
InitLinkList(&L); 
memset(CmpNum, 0, sizeof(CmpNum)); 
memset(ChgNum, 0, sizeof(ChgNum)); 
do{ 
SelectSort(); 
for (i = 0; i < MAXSIZE; i++) 
L.r[i].key = RandArray[i]; //随机数列复位 
scanf("%d", &select); 
switch (select){ 
case 1: 
printf("\n插入排序:\n"); 
TempTime = (int)GetTickCount(); 
ShellSort(&L, Indata, 1, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", 
CmpNum[select],ChgNum[select], SpendTime); 
break; 
case 2: 
printf("\n希尔排序:\n"); 
TempTime = (int)GetTickCount(); 
ShellSort(&L, dlta, 3, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", 
CmpNum[select],ChgNum[select], SpendTime); 
break; 
case 3: 
printf("\n快速排序:\n"); 
TempTime = (int)GetTickCount(); 
QuickSort(&L, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", 
CmpNum[select],ChgNum[select], SpendTime); 
break; 
case 4: 
printf("\n堆排序:\n"); 
TempTime = (int)GetTickCount(); 
HeapSort(&L, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\t\tTimeSpent=%dms\n",CmpNum[select], 
ChgNum[select], SpendTime); 
break; 
case 5: 
printf("\n冒泡排序:\n"); 
TempTime = (int)GetTickCount(); 
BubbleSort(&L, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", 
CmpNum[select],ChgNum[select], SpendTime); 
break; 
case 6: 
printf("\n选择排序:\n"); 
TempTime = (int)GetTickCount(); 
SelSort(&L, &CmpNum[select], &ChgNum[select]); 
SpendTime = (int)GetTickCount() - TempTime; 
for(i=1;i<=MAXSIZE;i++){ 
printf("%5d ",L.r[i].key); 
if(++j%10==0)printf("\n"); 
} 
printf("\n\nCompareNumber=%d\tChageNumber=%d\tTimeSpent=%dms\n", 
CmpNum[select],ChgNum[select], SpendTime); 
break; 
case 7: 
AllAbove(&L, CmpNum, ChgNum); 
break; 
} 
} 
while (select != 8 ); 
Display(&L); 
} 


答疑群:675511725

你可能感兴趣的:(数据结构)