比较两种随机算法的性能

1.实现RANDOMIZE-IN-PLACE随机排列算法与PERMUTE-BYSORTING算法,并比较效率。

思路描述

将两种取随机数算法分别作为两个函数写在i同一个程序里面

调整:

  • 我们一共需要一个多长的随机序列:source code中用size进行描述?

  • 我们将这个算法运行多少次 ,再source code中使用count_times 进行描述?

手动调整参数,分析对比

分析结果

size = 10 ; count_times = 10000

5c83684e6de2d

size = 1000 , count_time = 100

5c8368bba1a5f

size = 100 count = 1000

5c8368f8d9e59

结论

就目前而言,无论在什么情况下RANDOMIZE-IN-PLACE

算法都要较快一些

Source Code

// random.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include 
#include
#include
using namespace std;
void show_vector_int(vectoraim)
{
    for (int i = 0; i < aim.size(); i++)
        cout << aim[i]<<" ";
    cout << endl;
}

void get_random_rank_by_sorting(int size, vector& result,int bias)
{
    int n = size * size*size;
    srand((unsigned)time(0) + bias);
    for (int i = 0; i < size; i++)
    {

        result.push_back(rand() % n);
    }
    //show_vector_int(result);
    return;

}
void get_random_by_exchange(vector&result,int size,int bias)
{
    srand((unsigned)time(0)+bias);
    for (int i = 0; i < size; i++)
    {
        int aim = rand() % size;
        int tem = result[i];
        result[i] = result[aim];
        result[aim] = tem;
    }
    //show_vector_int(result);
    return;
}
int main()
{
    int size = 10;
    int count_times = 10000;
    cout << "one rank size is" << size << endl;
    cout << "run cycle is" << count_times << endl;
    clock_t start_time1, start_time2, end_time1, end_time2;
    start_time1 = clock();
    for (int i = 0; i < count_times; i++)
    {
        vectorresult;
        get_random_rank_by_sorting(size, result,i);
    }
    end_time1 = clock();
    cout << "the by_sorting time is about " << end_time1 - start_time1   << endl;
    vectorinitial;
    for (int i = 0; i < size; i++)
        initial.push_back(i);
    start_time2 = clock();
    for(int i = 0; i < count_times; i++)
    {
        vectorresult = initial;
        get_random_by_exchange(result, size,i);
    }
    end_time2 = clock();
    cout<< "the by_exchange_time is about " << end_time2 - start_time2 << endl;


    //std::cout << "Hello World!\n"; 
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值