-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSelectionSorting.cpp
More file actions
34 lines (27 loc) · 1.05 KB
/
Copy pathSelectionSorting.cpp
File metadata and controls
34 lines (27 loc) · 1.05 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
#include <vector>
#include <iostream>
template<typename Collection, typename Comparator, typename = typename Collection::iterator>
void selection_sorting(Collection& collection, Comparator comparator) noexcept
{
for (typename Collection::size_type i = 0; i < collection.size() - 1; i++)
{
typename Collection::size_type key_element_index = i;
for (typename Collection::size_type j = i + 1; j < collection.size(); j++)
if (comparator(collection[j], collection[key_element_index]))
key_element_index = j;
if (key_element_index != i)
std::swap(collection[i], collection[key_element_index]);
}
}
int main()
{
std::vector<int> vector = { 4, 7, 1, 5, 2, 9, 4, 7, 2, 9, 4 };
std::cout << "Not sorted array: ";
for (const auto& value : vector)
std::cout << value << " ";
selection_sorting(vector, [](int a, int b) { return a > b; });
std::cout << "\nSorted array: ";
for (const auto& value : vector)
std::cout << value << " ";
return EXIT_SUCCESS;
}