实现随机生成数组的算法

时间:2017-04-11 18:47:57

标签: c++ arrays sorting random

所以我有以下代码来生成一个由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;
	}
}

1 个答案:

答案 0 :(得分:2)

我不确定我是否帮助你,但你可以做的是在调用排序功能之前和之后测量时间。对于时间测量,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