-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionsort.cpp
60 lines (48 loc) · 1.49 KB
/
selectionsort.cpp
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
54
55
56
57
58
59
60
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <chrono>
void selection_sort(double * array, int length){
for(int i = 0; i < length; ++i){
//find min index to swap with
double m = array[i];
int idx = i;
//search from current element aka. unsorted part of the array
for(int j = i; j < length; ++ j){
if(array[j]< m){
m = array[j];
idx = j;
}
}
//swap min and curren (i and idx)
array[idx] = array[i];
array[i] = m;
}
}
int main(int argc, char ** argv){
int tests[11] = {10, 100, 500, 1000, 1500, 2000, 2500, 3000, 5000, 7500, 10000};
int m = 11;
double * array = (double *) std::malloc(tests[m-1] * sizeof(double));
std::cout << "[";
for(int i = 0; i < m; ++i){
//random fill array
for(int k = 0; k < tests[i]; ++k){
array[k] = (double) std::rand()/RAND_MAX;
}
//start times
auto start = std::chrono::high_resolution_clock::now();
//sort array
selection_sort(array, tests[i]);
//stop timer
auto end = std::chrono::high_resolution_clock::now();
//std::cout << std::endl;
//std::cout << tests[i] << " elements sorted in: " << (double) std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count()/1000000000 << "s" << std::endl;
std::cout << (double) std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count()/1000000000 << ", ";
}
std::cout << "\b\b]" << std::endl;
std::free(array);
}
/** Run with:
g++ selectionsort.cpp -o main.bin
./main.bin
*/