-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (67 loc) · 1.79 KB
/
main.cpp
File metadata and controls
79 lines (67 loc) · 1.79 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
74
75
76
77
78
79
#include <iostream>
#include <array>
#include <list>
#include <functional>
#include <assert.h>
#include "sort_algo.hpp"
using namespace std;
template<typename TStream, typename TIter>
void PrintCollection(std::string const & title, TStream & stream, TIter const & beg, TIter const & end)
{
stream << title;
static_assert(iter::ForwardIterCheck<TIter>::value, "");
for (TIter element = beg; element != end; ++element)
stream << *element << " ";
stream << std::endl;
}
template<size_t N>
std::array<int, N> RandomArray(int maxValue)
{
std::array<int, N> arr;
for (int & element : arr)
element = std::rand() % maxValue;
return arr;
};
enum ESortMethod
{
SORT_BUBBLE,
SORT_SELECTION,
SORT_INSERTION,
SORT_MERGE
};
void SortArrayTest(std::string const & title, ESortMethod method)
{
size_t const COLLECTION_SIZE = 30;
int const maxValue = 100;
{
using TArray = std::array<int, COLLECTION_SIZE>;
TArray arr = RandomArray<std::tuple_size<TArray>::value>(maxValue);
TArray::iterator b = begin(arr);
TArray::iterator e = end(arr);
switch (method)
{
case SORT_BUBBLE: sort::BubbleSort(b, e);
break;
case SORT_SELECTION: sort::SelectionSort(b, e);
break;
case SORT_INSERTION: sort::InsertionSort(b, e);
break;
case SORT_MERGE: sort::MergeSort(b, e);
break;
default:
assert(false);
}
PrintCollection(title, std::cout, begin(arr), end(arr));
}
}
int main()
{
using TClock = std::chrono::system_clock;
std::srand(TClock::now().time_since_epoch().count());
//std::srand(50);
SortArrayTest("Bubble sort : ", SORT_BUBBLE);
SortArrayTest("Selection sort : ", SORT_SELECTION);
SortArrayTest("Insertion sort : ", SORT_INSERTION);
SortArrayTest("Merge sort : ", SORT_MERGE);
return 0;
}