临近期末,学了一学期“数据结构”让写课程设计,就选了“内部排序算法的性能分析”。
主要是分析关键字的移动次数和比较次数,其中关键字若发生交换,则记为三次移动。下列代码中,“move”记为关键字的移动次数,“num”记为关键字的比较次数。比较的是对相同的数据,不同排序算法的效率高低。
下列代码中涉及六种排序算法,分别是:冒泡排序、直接排序、简单选择排序、快速排序、希尔排序、堆排序。在具体的运行过程中,希尔排序可能会出现不能执行的问题,具体是怎么出现的我也没弄清楚,有可能是编译器的问题,时而能执行,时而不能。在产生随机数的函数中,我设置的是产生一千以内的随机数。
稍后,会将这次的课程设计实验报告也放出来。
#include<windows.h>
#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
#define MAXSIZE 1005
typedef int KeyType;
typedef char InfoType[256];
long long int move=0;
int a[MAXSIZE] = {0};
int *d = a;
static long long move1=0;//移动次数
static long long move2=0;
static long long move3=0;
static long long move4=0;
static long long move5=0;
static long long move6=0;
long long num1 = 0;//比较次数
long long num2 = 0;
long long num3 = 0;
long long num4 = 0;
long long num5 = 0;
long long num6 = 0;
typedef struct
{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
}SqList;
Status InitList(SqList &L)
{
L.length = 0;
return 0;
}
Status CreateList(SqList &L, int n, int *d)
{
if(!L.r||n<1||n>MAXSIZE)
return ERROR;
for(int i=1;i<=n;i++)
L.r[i].key = d[i];
L.length=n;
return OK;
}
Status CreateList_1(SqList &L,SqList &l, int n)
{
for(int i=1; i<=n ; i++)
l.r[i].key = 0;
for(int i=1 ; i<=n ; i++)
l.r[i].key = L.r[i].key;
l.length = n;
}
void ListTraverse(SqList L, long long move, long long num)
{
for(int i=1;i<=L.length;i++)
{
cout<<L.r[i].key<<" ";
if(i%10 == 0)
cout<<endl;