-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
cycle_sort.cpp
131 lines (121 loc) · 3.99 KB
/
cycle_sort.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @file
* @brief Implementation of [Cycle
* sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
* @details
* Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in the best
* case and works in \f$O(n^2)\f$ in worst case. If a element is already at its
* correct position, do nothing. If a element is not at its correct position,
* we then need to move it to its correct position by computing the correct
* positions.Therefore, we should make sure the duplicate elements.
* @author [TsungHan Ho](https://github.com/dalaoqi)
*/
#include <algorithm> /// for std::is_sorted, std::swap
#include <cassert> /// for assert
#include <cstdint>
#include <iostream> /// for io operations
#include <vector> /// for std::vector
/**
* @namespace sorting
* @brief Sorting algorithms
*/
namespace sorting {
/**
* @namespace cycle_sort
* @brief Functions for [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort)
* algorithm
*/
namespace cycle_sort {
/**
* @brief The main function implements cycleSort
* @tparam T type of array
* @param in_arr array to be sorted
* @returns void
*/
template <typename T>
std::vector<T> cycleSort(const std::vector<T> &in_arr) {
std::vector<T> arr(in_arr);
for (int cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
// initialize item
T item = arr[cycle_start];
// Count the number of elements smaller than item, this number is the
// correct index of item.
int pos = cycle_start;
for (int i = cycle_start + 1; i < arr.size(); i++) {
if (arr[i] < item) {
pos++;
}
}
// item is already in correct position
if (pos == cycle_start) {
continue;
}
// duplicate elements
while (item == arr[pos]) pos += 1;
if (pos == cycle_start) {
continue;
} else {
std::swap(item, arr[pos]);
}
// Rest of the elements
while (pos != cycle_start) {
pos = cycle_start;
// Find position where we put the element
for (size_t i = cycle_start + 1; i < arr.size(); i++) {
if (arr[i] < item) {
pos += 1;
}
}
// duplicate elements
while (item == arr[pos]) pos += 1;
if (item == arr[pos]) {
continue;
} else {
std::swap(item, arr[pos]);
}
}
}
return arr;
}
} // namespace cycle_sort
} // namespace sorting
/**
* @brief Test implementations
* @returns void
*/
static void test() {
// Test 1
// [4, 3, 2, 1] return [1, 2, 3, 4]
std::vector<uint32_t> array1 = {4, 3, 2, 1};
std::cout << "Test 1... ";
std::vector<uint32_t> arr1 = sorting::cycle_sort::cycleSort(array1);
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
std::cout << "passed" << std::endl;
// [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
std::cout << "Test 2... ";
std::vector<double> arr2 = sorting::cycle_sort::cycleSort(array2);
assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
std::cout << "passed" << std::endl;
// Test 3
// [3, 3, 3, 3] return [3, 3, 3, 3]
std::vector<uint32_t> array3 = {3, 3, 3, 3};
std::cout << "Test 3... ";
std::vector<uint32_t> arr3 = sorting::cycle_sort::cycleSort(array3);
assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
std::cout << "passed" << std::endl;
// [9, 4, 6, 8, 14, 3] return [9, 4, 6, 8, 14, 3]
std::vector<uint32_t> array4 = {3, 4, 6, 8, 9, 14};
std::cout << "Test 4... ";
std::vector<uint32_t> arr4 = sorting::cycle_sort::cycleSort(array4);
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
std::cout << "passed" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // execute the test
return 0;
}