TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
selection_sort_recursive.cpp
Go to the documentation of this file.
1
30#include <algorithm>
31#include <cassert>
32#include <cstdint>
33#include <iostream>
34#include <vector>
35
40namespace sorting {
55template <typename T>
56uint64_t findMinIndex(const std::vector<T> &in_arr,
57 uint64_t current_position = 0) {
58 if (current_position + 1 == in_arr.size()) {
59 return current_position;
60 }
61 uint64_t answer = findMinIndex(in_arr, current_position + 1);
62 if (in_arr[current_position] < in_arr[answer]) {
63 answer = current_position;
64 }
65 return answer;
66}
67
75template <typename T>
76void selectionSortRecursive(std::vector<T> &in_arr,
77 uint64_t current_position = 0) {
78 if (current_position == in_arr.size()) {
79 return;
80 }
81 uint64_t min_element_idx =
82 selection_sort_recursive::findMinIndex(in_arr, current_position);
83 if (min_element_idx != current_position) {
84 std::swap(in_arr[min_element_idx], in_arr[current_position]);
85 }
86 selectionSortRecursive(in_arr, current_position + 1);
87}
88} // namespace selection_sort_recursive
89} // namespace sorting
90
95static void test() {
96 // 1st test
97 // [1, 0, 2, 1] return [0, 1, 1, 2]
98 std::vector<uint64_t> array1 = {0, 1, 1, 2};
99 std::cout << "1st test... ";
100 sorting::selection_sort_recursive::selectionSortRecursive(array1);
101 assert(std::is_sorted(std::begin(array1), std::end(array1)));
102 std::cout << "passed" << std::endl;
103 // 2nd test
104 // [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2]
105 std::vector<uint64_t> array2 = {1, 0, 0, 1, 1, 0, 2, 1};
106 std::cout << "2nd test... ";
107 sorting::selection_sort_recursive::selectionSortRecursive(array2);
108 assert(std::is_sorted(std::begin(array2), std::end(array2)));
109 std::cout << "passed" << std::endl;
110 // 3rd test
111 // [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2]
112 std::vector<uint64_t> array3 = {1, 1, 0, 0, 1, 2, 2, 0, 2, 1};
113 std::cout << "3rd test... ";
114 sorting::selection_sort_recursive::selectionSortRecursive(array3);
115 assert(std::is_sorted(std::begin(array3), std::end(array3)));
116 std::cout << "passed" << std::endl;
117 // 4th test
118 // [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2]
119 std::vector<uint64_t> array4 = {2, 2, 2, 0, 0, 1, 1};
120 std::cout << "4th test... ";
121 sorting::selection_sort_recursive::selectionSortRecursive(array4);
122 assert(std::is_sorted(std::begin(array4), std::end(array4)));
123 std::cout << "passed" << std::endl;
124}
125
130int main() {
131 test(); // run self-test implementations
132 return 0;
133}
Functions for the Selection sort implementation using recursion.
for working with vectors
uint64_t findMinIndex(const std::vector< T > &in_arr, uint64_t current_position=0)
The main function finds the index of the minimum element.
static void test()
Self-test implementations.
void selectionSortRecursive(std::vector< T > &in_arr, uint64_t current_position=0)
The main function implements Selection sort.
int main()
Main function.