| 
                        
                         本文是针对[数据结构基础系列(9):排序]的实践项目。 
【项目 - 大数据集上排序算法性能的体验】 
设计一个函数,产生一个至少5万条记录的数据集合。在同一数据集上大数据排序,用直接插入排序、冒泡排序、快速排序、直接选择排序、堆排序、归并排序、基数排序等算法进行排序,记录所需要的时间,经过对比,得到对复杂度不同的各种算法在运行时间方面的感性认识。 
提示1:这一项目需要整合多种排序算法,可以考虑先建设排序算法库,作为我们这门课算法库的收官之作; 
提示2:本项目旨在获得对于复杂度不同算法的感性认识,由于数据分布特点、计算机运行状态等不同,其结果并不能完全代替对算法复杂度的理论分析; 
提示3:由于C语言标准提供的时间函数只精确到秒,几种O(nlog2n)级别的算法,在5万条记录的压力下,并不能明显地看出优劣,可以忽略直接插入排序、冒泡排序、直接选择排序这三种相对低效率的算法(以节约时间。若能够忍受他们长时间地运行,请自便。),成10倍地加大数据量,然后进行观察。 
[参考解答] 
1.测试用的主控程序——main.cpp 
void GetLargeData(RecType *&R, int n)
{
    srand(time(0));
    R=(RecType*)malloc(sizeof(RecType)*n);
    for(int i=0; irand();  //产生0~RAND_MAX间的数
    printf("生成了%d条记录\n", n);
}
//调用某一排序算法完成排序,返回排序用时
long Sort(RecType *&R, int n, void f(RecType*, int))
{
    int i;
    long beginTime, endTime;
    RecType *R1=(RecType*)malloc(sizeof(RecType)*n);
    for (i=0;itime(0);
    f(R1,n);
    endTime = time(0);
    free(R1);
    return endTime-beginTime;
}
//调用基数排序算法完成排序,返回排序用时
long Sort1(RecType *&R, int n)
{
    long beginTime, endTime;
    RadixRecType *p;
    CreateLink(p,R,n);
    beginTime = time(0);
    RadixSort(p);
    endTime = time(0);
    DestoryLink(p);
    return endTime-beginTime;
}
int main()
{
    RecType *R;
    int n = MaxSize;   //测试中, MaxSize取50W
    GetLargeData(R, n);
    printf("各种排序花费时间:\n");
    printf("  直接插入排序:%ld\n", Sort(R, n, InsertSort));
    printf("  希尔排序:%ld\n", Sort(R, n, ShellSort));
    printf("  冒泡排序:%ld\n", Sort(R, n, BubbleSort));
    printf("  快速排序:%ld\n", Sort(R, n, QuickSort));
    printf("  直接选择排序:%ld\n", Sort(R, n, SelectSort));
    printf("  堆排序:%ld\n", Sort(R, n, HeapSort));
    printf("  归并排序:%ld\n", Sort(R, n, MergeSort));
    printf("  基数排序:%ld\n", Sort1(R, n));
    free(R);
    return 0;
} 
2.头文件 —— sort.h 
#ifndef SORT_H_INCLUDED
#define SORT_H_INCLUDED
#define MaxSize 50000      
#define Radix 10           
#define Digits 10          
typedef int KeyType;    
typedef char InfoType[10];
typedef struct          
{
    KeyType key;        
    InfoType data;      
} RecType;              
typedef struct node
{
    KeyType data;      
    struct node *next;
} RadixRecType;
void InsertSort(RecType R[],int n); 
void ShellSort(RecType R[],int n);  
void BubbleSort(RecType R[],int n); 
void QuickSort(RecType R[],int n);  
void SelectSort(RecType R[],int n);  
void HeapSort(RecType R[],int n);  
void MergeSort(RecType R[],int n); 
void CreateLink(RadixRecType *&p,RecType R[],int n);   
void DestoryLink(RadixRecType *&p); 
void RadixSort(RadixRecType *&p); 
#endif 
 
3.算法的实现—— sort.cpp 
#include "sort.h"
#include 
void InsertSort(RecType R[],int n)
{
    int i,j;
    RecType tmp;
    for (i=1; i1;            
        while (j>=0 && tmp.key1]=R[j]; 
            j--;
        }
        R[j+1]=tmp;      
    }
}
void ShellSort(RecType R[],int n)
{
    int i,j,gap;
    RecType tmp;
    gap=n/2;                
    while (gap>0)
    {
        for (i=gap; i 
                                                (编辑:成都站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |