-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
111 lines (97 loc) · 2.57 KB
/
Copy pathexample.cpp
File metadata and controls
111 lines (97 loc) · 2.57 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @file example.cpp
* @brief Show how to use a sorting algorithm, using Bubble Sort as an example.
* @author MingMinNa
*/
#include "include/CppSortingAlgos/bubble_sort.hpp"
#include <vector>
#include <iostream>
#include <iomanip>
#include <type_traits>
struct Element {
int key;
int value;
};
/* Comparators */
// note that the comparators must be either strictly increasing or strictly decreasing.
auto less_cmp = [](const Element& a, const Element& b) {
return a.key < b.key;
};
auto greater_cmp = [](const Element& a, const Element& b) {
return a.key > b.key;
};
/* Pretty print */
void print(const std::vector<Element>& v, const std::string& title)
{
std::cout << title << "\n";
for (const auto& e : v) {
std::cout << "key = " << std::setw(2) << e.key
<< ", value = " << e.value << "\n";
}
std::cout << "----------------------\n";
}
/* Trait Printer */
template <typename Sort>
void print_traits(const std::string& name)
{
std::cout << "Traits of " << name << "\n";
std::cout << " is_stable : " << (Sort::is_stable ? "true" : "false") << "\n";
std::cout << " is_comparison : " << (Sort::is_comparison ? "true" : "false") << "\n";
std::cout << " in_place : " << (Sort::in_place ? "true" : "false") << "\n";
std::cout << "----------------------\n";
}
int main()
{
using Sort = sort_algo::BubbleSort;
std::vector<Element> data = {
{70, 1},
{30, 2},
{90, 3},
{10, 4},
{70, 5},
{50, 6},
{10, 7}
};
print_traits<Sort>(Sort::name);
print(data, "Original");
Sort::sort<Element>(data, less_cmp);
print(data, "Sorted (Ascending)");
Sort::sort<Element>(data, greater_cmp);
print(data, "Sorted (Descending)");
return 0;
}
/* Output as below */
/*
Traits of Bubble Sort
is_stable : true
is_comparison : true
in_place : true
----------------------
Original
key = 70, value = 1
key = 30, value = 2
key = 90, value = 3
key = 10, value = 4
key = 70, value = 5
key = 50, value = 6
key = 10, value = 7
----------------------
Sorted (Ascending)
key = 10, value = 4
key = 10, value = 7
key = 30, value = 2
key = 50, value = 6
key = 70, value = 1
key = 70, value = 5
key = 90, value = 3
----------------------
Sorted (Descending)
key = 90, value = 3
key = 70, value = 1
key = 70, value = 5
key = 50, value = 6
key = 30, value = 2
key = 10, value = 4
key = 10, value = 7
----------------------
*/