当前位置 |首页 > 代写教程 > C语言/C++ 教程 >
分享这个代写网站给同学或者朋友吧!

测试措施

后头的例程,都是对数组的排序,利用静态链表的也合用于链表的排序。为简朴起见,只对单要害码排序,而且最后的功效都是从新到尾按升序分列。下面是统一的测试措施:

#include <iostream>
#include <iomanip>
using namespace std;
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "InsertSort.h"
#define random(num) (rand() % (num))
#define randomize() srand((unsigned)time(NULL))
#define N 10000 //排序元素的数目
#define SORT InsertSort //排序要领
class timer//单元ms
{
public:
void start() { start_t = clock(); }
clock_t time() { return (clock() - start_t); }
private:
clock_t start_t;
};
int KCN, RMN; timer TIMER;
void test(int a[])
{
TIMER.start();
SORT<int>(a, N, KCN, RMN);
cout << "\tTimeSpared: " << TIMER.time() << "ms" << endl;
cout << "KCN=" << left << setw(11) << KCN;
cout << "KCN/N=" << left << setw(11)<< (double)KCN/N;
cout << "KCN/N^2=" << left << setw(11)<< (double)KCN/N/N;
cout << "KCN/NlogN=" << left << setw(11)<< (double)KCN/N/log((double)N)*log(2.0) << endl;
cout << "RMN=" << left << setw(11) << RMN;
cout << "RMN/N=" << left << setw(11)<< (double)RMN/N;
cout << "RMN/N^2=" << left << setw(11)<< (double)RMN/N/N;
cout << "RMN/NlogN=" << left << setw(11)<< (double)RMN/N/log((double)N)*log(2.0) << endl;
}
int main()
{
int i;
//randomize();为了在沟通环境下较量各个排序算法,不加这句
int* ascending = new int[N];//升序序列
int* descending = new int[N];//降序序列
int* randomness = new int[N];//随机序列
for (i = 0; i < N; i++) { ascending[i] = i; randomness[i] = i; descending[i] = N - i - 1;}
for (i = 0; i < N; i++) swap(randomness[i], randomness[random(N)]);
cout << "Sort ascending N=" << N; test(ascending);
cout << "Sort randomness N=" << N; test(randomness);
cout << "Sort descending N=" << N; test(descending);
return 0;
}

需要说明一点,KCN(要害码较量次数)、RMN(记录移动次数)并不是算法必需的,是为了对算法的机能有个直观的评价(不消那些公式算来算去)。对10000个整数排序应该是最省事的测试手段,发起不要再增多记录数目了,一是在最坏的环境不消等太久的时间,二是制止KCN、RMN溢出,别的有些递归的算法在环境较量糟的时候,记录数目太多仓库大概会溢出,导致措施瓦解。

代写