TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
selection_sort_iterative.cpp
1/******************************************************************************
2 * @file
3 * @brief Implementation of the [Selection
4 * sort](https://en.wikipedia.org/wiki/Selection_sort) implementation using
5 * swapping
6 * @details
7 * The selection sort algorithm divides the input vector into two parts: a
8 * sorted subvector of items which is built up from left to right at the front
9 * (left) of the vector, and a subvector of the remaining unsorted items that
10 * occupy the rest of the vector. Initially, the sorted subvector is empty, and
11 * the unsorted subvector is the entire input vector. The algorithm proceeds by
12 * finding the smallest (or largest, depending on the sorting order) element in
13 * the unsorted subvector, exchanging (swapping) it with the leftmost unsorted
14 * element (putting it in sorted order), and moving the subvector boundaries one
15 * element to the right.
16 *
17 * ### Implementation
18 *
19 * SelectionSort
20 * The algorithm divides the input vector into two parts: the subvector of items
21 * already sorted, which is built up from left to right. Initially, the sorted
22 * subvector is empty and the unsorted subvector is the entire input vector. The
23 * algorithm proceeds by finding the smallest element in the unsorted subvector,
24 * exchanging (swapping) it with the leftmost unsorted element (putting it in
25 * sorted order), and moving the subvector boundaries one element to the right.
26 *
27 * @author [Lajat Manekar](https://github.com/Lazeeez)
28 * @author Unknown author
29 *******************************************************************************/
30#include <algorithm>
31#include <cassert>
32#include <cstdint>
33#include <iostream>
34#include <vector>
35
36/******************************************************************************
37 * @namespace sorting
38 * @brief Sorting algorithms
39 *******************************************************************************/
40namespace sorting {
41/******************************************************************************
42 * @brief The main function which implements Selection sort
43 * @param arr vector to be sorted
44 * @param len length of vector to be sorted
45 * @returns @param array resultant sorted vector
46 *******************************************************************************/
47
48std::vector<uint64_t> selectionSort(const std::vector<uint64_t> &arr,
49 uint64_t len) {
50 std::vector<uint64_t> array(
51 arr.begin(),
52 arr.end()); // declare a vector in which result will be stored
53 for (uint64_t it = 0; it < len; ++it) {
54 uint64_t min = it; // set min value
55 for (uint64_t it2 = it + 1; it2 < len; ++it2) {
56 if (array[it2] < array[min]) { // check which element is smaller
57 min = it2; // store index of smallest element to min
58 }
59 }
60
61 if (min != it) { // swap if min does not match to i
62 uint64_t tmp = array[min];
63 array[min] = array[it];
64 array[it] = tmp;
65 }
66 }
67
68 return array; // return sorted vector
69}
70} // namespace sorting
71
72/*******************************************************************************
73 * @brief Self-test implementations
74 * @returns void
75 *******************************************************************************/
76static void test() {
77 // testcase #1
78 // [1, 0, 0, 1, 1, 0, 2, 1] returns [0, 0, 0, 1, 1, 1, 1, 2]
79 std::vector<uint64_t> vector1 = {1, 0, 0, 1, 1, 0, 2, 1};
80 uint64_t vector1size = vector1.size();
81 std::cout << "1st test... ";
82 std::vector<uint64_t> result_test1;
83 result_test1 = sorting::selectionSort(vector1, vector1size);
84 assert(std::is_sorted(result_test1.begin(), result_test1.end()));
85 std::cout << "Passed" << std::endl;
86
87 // testcase #2
88 // [19, 22, 540, 241, 156, 140, 12, 1] returns [1, 12, 19, 22, 140, 156,
89 // 241,540]
90 std::vector<uint64_t> vector2 = {19, 22, 540, 241, 156, 140, 12, 1};
91 uint64_t vector2size = vector2.size();
92 std::cout << "2nd test... ";
93 std::vector<uint64_t> result_test2;
94 result_test2 = sorting::selectionSort(vector2, vector2size);
95 assert(std::is_sorted(result_test2.begin(), result_test2.end()));
96 std::cout << "Passed" << std::endl;
97
98 // testcase #3
99 // [11, 20, 30, 41, 15, 60, 82, 15] returns [11, 15, 15, 20, 30, 41, 60, 82]
100 std::vector<uint64_t> vector3 = {11, 20, 30, 41, 15, 60, 82, 15};
101 uint64_t vector3size = vector3.size();
102 std::cout << "3rd test... ";
103 std::vector<uint64_t> result_test3;
104 result_test3 = sorting::selectionSort(vector3, vector3size);
105 assert(std::is_sorted(result_test3.begin(), result_test3.end()));
106 std::cout << "Passed" << std::endl;
107
108 // testcase #4
109 // [1, 9, 11, 546, 26, 65, 212, 14, -11] returns [-11, 1, 9, 11, 14, 26, 65,
110 // 212, 546]
111 std::vector<uint64_t> vector4 = {1, 9, 11, 546, 26, 65, 212, 14};
112 uint64_t vector4size = vector2.size();
113 std::cout << "4th test... ";
114 std::vector<uint64_t> result_test4;
115 result_test4 = sorting::selectionSort(vector4, vector4size);
116 assert(std::is_sorted(result_test4.begin(), result_test4.end()));
117 std::cout << "Passed" << std::endl;
118}
119
120/*******************************************************************************
121 * @brief Main function
122 * @returns 0 on exit
123 *******************************************************************************/
124int main() {
125 test(); // run self-test implementations
126 return 0;
127}
void test()
int main()
Main function.
for working with vectors