实现随机生成数组的算法
Implementing algorithms for Randomly generated array
所以我有以下代码来生成一个由 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
相关文章:
- 为什么我的排序算法会更改数组值
- 使用指针算法修改函数中的 2D 数组
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 通过指针算法计算数组长度
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- 为什么在此排序算法实现中,向量明显比数组慢?
- C++线性搜索算法,确定数组中元素的数量
- 在矢量数组C++中使用算法库操作
- 数组 X[n] 仅存储名称.编写算法以在数组中插入或删除 ITEM
- 使用简单的暴力算法找到数组中最大的4个元素
- 计算数组中存在其总和的对数的算法
- 我是 c++ 的新手,有没有一种算法可以找到 3d 数组中最接近的 0 的距离?
- 使用指针算法调用结构内的结构数组
- 使用动态数组对算法编译器错误进行排序
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- 你不能在 void 指针上使用指针算法,那么 void 指针数组是如何工作的呢?
- 使用指针算法搜索 2D 数组
- 我的算法在 1D 数组中渲染矩形有什么问题
- 通过分而治算法计算数组的最大数量
- 优化所需算法-数组