-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
73 lines (57 loc) · 1.68 KB
/
insertion_sort.cpp
File metadata and controls
73 lines (57 loc) · 1.68 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
template<typename It, class Compare>
void insertion_sort(It beg, It end, Compare comp)
{
auto length = distance(beg, end);
auto arr = beg;
for (int i=1; i<length; i++)
for (int j=i; j>0 && comp(arr[j], arr[j-1]); j--)
swap(arr[j], arr[j-1]);
}
void p(const vector<int>& arr)
{
for (int i=0; i<10; i++) cout << arr[i] << " ";
cout << endl;
}
template<class Compare>
void test_insertion(vector<int> arr, Compare comp)
{
auto t0 = chrono::high_resolution_clock::now();
insertion_sort(arr.begin(), arr.end(), comp);
auto t1 = chrono::high_resolution_clock::now();
auto d = chrono::duration<double, milli>(t1 - t0).count();
cout << d << endl;
p(arr);
}
template<class Compare>
void test_stdsort(vector<int> arr, Compare comp)
{
auto t0 = chrono::high_resolution_clock::now();
sort(arr.begin(), arr.end(), comp);
auto t1 = chrono::high_resolution_clock::now();
auto d = chrono::duration<double, milli>(t1 - t0).count();
cout << d << endl;
p(arr);
}
int main(int argc, char *argv[])
{
const int SZ = 30;
srand(time(nullptr));
vector<int> arr = { 30, 28, 39, 27, 26, 25, 24, 23, 22, 21,
20, 18, 19, 17, 16, 15, 14, 13, 12, 11,
8, 9, 7, 6, 5, 4, 3, 2, 1, 0 };
//for (auto i=0; i<SZ; i++) arr.push_back(rand() % SZ);
// for (auto i=0; i<SZ; i++) arr.push_back(i);
for (auto i: arr) cout << i << " ";
cout << endl;
//for (auto i=0; i<SZ; i++) arr.push_back(SZ-i);
test_insertion(arr, greater<int>());
test_stdsort (arr, greater<int>());
return 0;
}