【问题标题】:Implementing algorithms for Randomly generated array实现随机生成数组的算法
【发布时间】:2017-04-11 18:47:56
【问题描述】:

所以我有以下代码来生成一个由 1-20000 的随机数组成的数组,并且该数组可以有不同的大小(100、500、1000 等)。

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <ctime>

using namespace std;


void insertSort(int input[]);
/*
void quickSort(int input[], int, int);
void mergeSort(int input[], int, int);
void merge(int input[], int, int, int);
void percolate(int numbers[], int, int);
void heapSort(int numbers[], int);
*/
int SIZE; //global Size variable


int main()
{
      int i;
      //int size;
      int random_once[10000]; //maximum array size

      srand(time(0));

      cout << "How big would you like the list to be?" << "\n\n";
      cout << "\t Enter any of the following: 100, 500, 1000, 2000, 5000, 8000, or 10000" << "\n";
      cin >> SIZE;

      for (int i=0; i<SIZE; i++)
      {
          random_once[i]=rand() % 20000; //generating a random number between 1 - 20000

          // generate unique random number only once so no duplicates
          for(int j=0; j<i; j++) if (random_once[j]==random_once[i]) i--;

      }
      cout<< " " << i << "\n\n\n ";
      for(i=0; i<SIZE; i++) cout << " " << random_once[i] << "\t";  //outputting array to console

  return 0;
}



//insert sort algorithm
void insertSort(int input[]) {
    int i, j, temp;
    for (i = 0; i < SIZE; i++) {
        temp = input[i];
        for (j = i; j > 0 && temp < input[j - 1]; j--) {
            input[j] = input[j - 1];
        }
        input[j] = temp;
    }
    return;
}

我想知道如何打印出插入排序,以便在使用插入排序算法后打印出排序后的数组。

我还想知道如何在排序之前和之后打印出算法的时间复杂度,以便与其他排序算法进行比较。

编辑(添加模板)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <string>

using namespace std;

void insertSort(int input[]);
/*
void quickSort(int input[], int, int);
void mergeSort(int input[], int, int);
void merging(int input[], int, int, int);
void percolate(int numbers[], int, int);
void heapSort(int numbers[], int);
*/
int SIZE; //global Size variable


template <typename F, typename... Args>
std::string callTime(F func, int array[], Args... args) {
    using namespace chrono;
    auto t1 = high_resolution_clock::now();
    func(array, args...);
    auto t2 = high_resolution_clock::now();
    return to_string(duration_cast<milliseconds>(t2 - t1).count()) + "ms\n";
}

int main()
{

      int i;
      //int size;
      int random_once[10000]; //maximum array size

      srand(time(0));

      //asking user for the size of the list
      cout << "How big would you like the list to be?" << "\n\n";
      cout << "\t Enter any of the following: 100, 500, 1000, 2000, 5000, 8000, or 10000" << "\n";
      cin >> SIZE;

      //for loop to generate numbers and nested for loop to handle duplicates
      for (int i=0; i<SIZE; i++)
      {
          random_once[i]=rand() % 20000; //generating a random number between 1 - 20000

          // generate unique random number only once so no duplicates
          for(int j=0; j<i; j++) if (random_once[j]==random_once[i]) i--;

      }//end generating for loop
      cout<< " " << i << "\n\n\n ";
      for(i=0; i<SIZE; i++) cout << " " << random_once[i] << "\t";  //outputting array to console

      cout << "insertSort(): " << callTime(&insertSort, random_once);
  return 0;
} //end main




//insert sort algorithm
void insertSort(int input[]) {
	int i, j, temp;
	for (i = 0; i < SIZE; i++) {
		temp = input[i];
		for (j = i; j > 0 && temp < input[j - 1]; j--) {
			input[j] = input[j - 1];
		}
		input[j] = temp;
	}
}

【问题讨论】:

  • 没有getAlgorithmComplexity() 函数。这是您通过查看代码来断言的内容。
  • 只需要得到算法的时间复杂度吗?
  • 在运行算法之前使用clock 来节省时间,在算法之后获取时间,它们之间的区别在于所花费的时间。如果我明白了你的要求。
  • 大多数排序算法都允许您提供自定义比较器。在这个比较器中,你可以计算它被调用了多少次。
  • @MarekChocholáček 因此,我可以输出算法的时间复杂度,以及如何设置 insertSort 算法,它会自动运行它,它只是不输出排序后的数组。 (我认为不需要)但算法在后台运行正确吗?

标签: c++ arrays sorting random


【解决方案1】:

我不确定我是否正确,但您可以做的是在调用排序函数之前和之后测量时间。对于时间测量,C++ 有各种工具,例如我在下面的代码中使用的 std::high_resolution_clock。然后,您可以观察排序算法在不同大小的数组上的行为。在那里我给你写了模板函数,它应该适用于你计划实现的每一种类型(不要忘记包含 chrono 标题):

#include <chrono>

template <typename F, typename... Args>
std::string callTime(F&& func, int array[], Args&&... args) {
    using namespace std::chrono;
    auto t1 = high_resolution_clock::now();
    func(array, std::forward<Args>(args)...);
    auto t2 = high_resolution_clock::now();
    return std::to_string(duration_cast<milliseconds>(t2 - t1).count()) + "ms\n";
}

你可以这样称呼它:

int main() {
    // what you have before
    cout << "insertSort(): " << callTime(insertSort, random_once);
    //...
    cout << "merge(): " << callTime&merge, random_once, /* other params */);
    //...
    return 0;
}

请注意,您可以更改持续时间投射的精度:

duration_cast<microseconds>(t2 - t1).count() // now returns microseconds passed

【讨论】:

  • @RileyKennedy 如果您有任何问题,请随时提问
  • 我假设该功能暂时属于我的插入排序功能?
  • 我收到这个错误:|在函数'std::string callTime(F, int*, Args ...)':| |32|错误:“to_string”没有依赖于模板参数的参数,因此“to_string”的声明必须可用|在“std::string callTime(F, int*, Args ... ) [with F = void ()(int);参数 = {}; std::string = std::basic_string]':| |61|从这里需要| |32|错误:'to_string' 未在此范围内声明|
  • 我已经在原始帖子中添加了一个关于代码现在看起来如何的编辑,并添加了您给我的内容!
  • @RileyKennedy 包含字符串标题。您还在排序之前打印数组。在 callTime 函数中调用 insertSort。
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 2012-08-10
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 2020-03-05
相关资源
最近更新 更多