TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
cycle_sort.cpp
Go to the documentation of this file.
1
14#include <algorithm>
15#include <cassert>
16#include <cstdint>
17#include <iostream>
18#include <vector>
19
24namespace sorting {
30namespace cycle_sort {
37template <typename T>
38std::vector<T> cycleSort(const std::vector<T> &in_arr) {
39 std::vector<T> arr(in_arr);
40 for (int cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
41 // initialize item
42 T item = arr[cycle_start];
43
44 // Count the number of elements smaller than item, this number is the
45 // correct index of item.
46 int pos = cycle_start;
47 for (int i = cycle_start + 1; i < arr.size(); i++) {
48 if (arr[i] < item) {
49 pos++;
50 }
51 }
52
53 // item is already in correct position
54 if (pos == cycle_start) {
55 continue;
56 }
57
58 // duplicate elements
59 while (item == arr[pos]) pos += 1;
60 if (pos == cycle_start) {
61 continue;
62 } else {
63 std::swap(item, arr[pos]);
64 }
65 // Rest of the elements
66 while (pos != cycle_start) {
67 pos = cycle_start;
68 // Find position where we put the element
69 for (size_t i = cycle_start + 1; i < arr.size(); i++) {
70 if (arr[i] < item) {
71 pos += 1;
72 }
73 }
74 // duplicate elements
75 while (item == arr[pos]) pos += 1;
76 if (item == arr[pos]) {
77 continue;
78 } else {
79 std::swap(item, arr[pos]);
80 }
81 }
82 }
83 return arr;
84}
85} // namespace cycle_sort
86} // namespace sorting
87
92static void test() {
93 // Test 1
94 // [4, 3, 2, 1] return [1, 2, 3, 4]
95 std::vector<uint32_t> array1 = {4, 3, 2, 1};
96 std::cout << "Test 1... ";
97 std::vector<uint32_t> arr1 = sorting::cycle_sort::cycleSort(array1);
98 assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
99 std::cout << "passed" << std::endl;
100
101 // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
102 std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
103 std::cout << "Test 2... ";
104 std::vector<double> arr2 = sorting::cycle_sort::cycleSort(array2);
105 assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
106 std::cout << "passed" << std::endl;
107
108 // Test 3
109 // [3, 3, 3, 3] return [3, 3, 3, 3]
110 std::vector<uint32_t> array3 = {3, 3, 3, 3};
111 std::cout << "Test 3... ";
112 std::vector<uint32_t> arr3 = sorting::cycle_sort::cycleSort(array3);
113 assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
114 std::cout << "passed" << std::endl;
115
116 // [9, 4, 6, 8, 14, 3] return [9, 4, 6, 8, 14, 3]
117 std::vector<uint32_t> array4 = {3, 4, 6, 8, 9, 14};
118 std::cout << "Test 4... ";
119 std::vector<uint32_t> arr4 = sorting::cycle_sort::cycleSort(array4);
120 assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
121 std::cout << "passed" << std::endl;
122}
123
128int main() {
129 test(); // execute the test
130 return 0;
131}
static void test()
Test implementations.
int main()
Main function.
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Functions for Cycle sort algorithm.
for working with vectors