实现随机生成数组的算法

Implementing algorithms for Randomly generated array

本文关键字:算法 数组 随机 实现      更新时间:2023-10-16

所以我有以下代码来生成一个由 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?" << "nn";
      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 << "nnn ";
      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()) + "msn";
}
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?" << "nn";
      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 << "nnn ";
      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;
	}
}

我不确定我是否正确,但您可以做的是测量调用排序函数之前和之后的时间。对于时间测量,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()) + "msn";
}

你可以这样称呼它:

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