实习四 内部排序算法的性能测试 联系客服

发布时间 : 星期三 文章实习四 内部排序算法的性能测试更新完毕开始阅读90ebcc6059eef8c75ebfb300

4.用户手册

运行程序后,上面会提示你想用几组不同的输入数据作比较(题目要求至少用5组),输入你想要比较的次数后,其会按照冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、堆排序的顺序显示出各自算法的移动次数和比较次数。

5.测试结果

6.源代码

………………………………………….order.h………………………………………………….

typedef int KeyType ; typedef struct { KeyType key; }DataType; typedef struct {

int compare;//比较次数 int move;//移动次数

//定义及初始化成员变量 }Perf; //冒泡排序

Perf BubbleSort(DataType a[],int n)

{ Perf bu;

bu.compare = 0;//开始时比较次数置为0 bu.move = 0;//开始时移动次数置为0 int i,j,flag=1;//flag用于标记本次交换排序过程是否有交换动作 DataType temp; for(i=1;ia[j+1].key) { flag=1; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; bu.move = bu.move+3;//移动次数 } bu.compare++;//比较次数 } } return bu; }

//直接插入排序

Perf InsertSort(DataType a[],int n) { int i,j; DataType temp; Perf in; in.compare = 0; in.move = 0; for(i=0;i-1&&temp.key

a[j+1]=temp; in.compare++; in.move++; } return in; }

//简单选择排序

Perf SelectSort(DataType a[],int n) { int i,j,small; DataType temp; Perf se;

se.compare = 0; se.move = 0; for(i=0;i

void QuickSort(DataType a[],int low,int high,int &c,int &m) { int i=low,j=high; DataType temp=a[low];//取第一个元素为标准数据元素 m++; while(i