-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstalin_sort.hpp
60 lines (50 loc) · 1.29 KB
/
stalin_sort.hpp
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
// algocpp/algorithm/stalin_sort.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_ALGORITHM_STALIN
#define ALGOCPP_ALGORITHM_STALIN
#include <vector>
#include <array>
#include <list>
#include <algocpp/type/to_vector.hpp>
namespace algocpp
{
namespace algorithm
{
namespace base
{
template <typename T>
inline std::vector<T> base_stalin_sort(std::vector<T> v, bool up)
{
if (v.size() == 0)
return std::vector<T>{};
std::vector<T> result = {v[0]};
for (unsigned long long i = 1; i < v.size(); i++)
{
if ((up && result.back() < v[i]) || (!up && result.back() <= v[i]))
{
result.emplace_back(v[i]);
}
}
return result;
}
}
template <typename T>
inline std::vector<T> stalin_sort(std::vector<T> v, bool up = false)
{
return base::base_stalin_sort(v, up);
}
template <typename T>
inline std::vector<T> stalin_sort(std::list<T> v, bool up = false)
{
return base::base_stalin_sort(algocpp::type::to_vector(v), up);
}
template <typename T, std::size_t n>
inline std::vector<T> stalin_sort(std::array<T, n> v, bool up = false)
{
return base::base_stalin_sort(algocpp::type::to_vector(v), up);
}
}
}
#endif // ALGOCPP_ALGORITHM_STALIN