-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortHelper.h
53 lines (39 loc) · 968 Bytes
/
SortHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef SORTHELPER
#define SORTHELPER
#include<iostream>
#include<cassert>
using namespace std;
namespace SortHelper {
int* generatRandomArray(int n, int rangeL, int rangeR) {
assert(rangeR >= rangeL);
int* arr = new int[n];
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % (rangeR - rangeL + 1) + rangeL;
}
return arr;
}
template<typename T>
bool isSorted(T arr[], int n) {
for (int i = 0; i < n-1; i++) {
if (arr[i]>arr[i + 1]){
return false;
}
}
return true;
}
template<typename T>
void testSort(string sortname, void(*sort)(T[], int), T arr[] ,int n) {
clock_t startime = clock();
sort(arr,n);
clock_t endtime = clock();
/*for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}*/
assert(isSorted(arr,n));
cout << sortname << ":" << double(endtime - startime) / CLOCKS_PER_SEC << endl;
//
return;
}
}
#endif // SORTHELPER