【发布时间】: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 算法,它会自动运行它,它只是不输出排序后的数组。 (我认为不需要)但算法在后台运行正确吗?