mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
tests: Add test in cycle_sort.cpp
(#1520)
* Update cycle_sort.cpp * Update sorting/cycle_sort.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update sorting/cycle_sort.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * clang-format and clang-tidy fixes for2601c621
* Update cycle_sort.cpp * Update cycle_sort.cpp * Update cycle_sort.cpp * clang-format and clang-tidy fixes for39cf3f34
* Update sorting/cycle_sort.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update cycle_sort.cpp * Update cycle_sort.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
394811e601
commit
82290e72c5
@ -2,14 +2,12 @@
|
|||||||
* @file
|
* @file
|
||||||
* @brief Implementation of [Cycle
|
* @brief Implementation of [Cycle
|
||||||
* sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
|
* sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
|
||||||
*
|
|
||||||
* @details
|
* @details
|
||||||
* Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in best cas
|
* Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in the best
|
||||||
* and works in \f$O(n^2)\f$ in worst case. If a element is already at its
|
* 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,
|
* 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
|
* we then need to move it to its correct position by computing the correct
|
||||||
* positions.Therefore, we should make sure the duplicate elements.
|
* positions.Therefore, we should make sure the duplicate elements.
|
||||||
*
|
|
||||||
* @author [TsungHan Ho](https://github.com/dalaoqi)
|
* @author [TsungHan Ho](https://github.com/dalaoqi)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -38,14 +36,14 @@ namespace cycle_sort {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
||||||
std::vector<T> arr(in_arr);
|
std::vector<T> arr(in_arr);
|
||||||
for (size_t cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
|
for (int cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
|
||||||
// initialize item
|
// initialize item
|
||||||
T item = arr[cycle_start];
|
T item = arr[cycle_start];
|
||||||
|
|
||||||
// Count the number of elements smaller than item, this number is the
|
// Count the number of elements smaller than item, this number is the
|
||||||
// correct index of item.
|
// correct index of item.
|
||||||
int pos = cycle_start;
|
int pos = cycle_start;
|
||||||
for (size_t i = cycle_start + 1; i < arr.size(); i++) {
|
for (int i = cycle_start + 1; i < arr.size(); i++) {
|
||||||
if (arr[i] < item) {
|
if (arr[i] < item) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
@ -58,8 +56,11 @@ std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
|||||||
|
|
||||||
// duplicate elements
|
// duplicate elements
|
||||||
while (item == arr[pos]) pos += 1;
|
while (item == arr[pos]) pos += 1;
|
||||||
|
if (pos == cycle_start) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
std::swap(item, arr[pos]);
|
std::swap(item, arr[pos]);
|
||||||
|
}
|
||||||
// Rest of the elements
|
// Rest of the elements
|
||||||
while (pos != cycle_start) {
|
while (pos != cycle_start) {
|
||||||
pos = cycle_start;
|
pos = cycle_start;
|
||||||
@ -71,9 +72,13 @@ std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
|||||||
}
|
}
|
||||||
// duplicate elements
|
// duplicate elements
|
||||||
while (item == arr[pos]) pos += 1;
|
while (item == arr[pos]) pos += 1;
|
||||||
|
if (item == arr[pos]) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
std::swap(item, arr[pos]);
|
std::swap(item, arr[pos]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
} // namespace cycle_sort
|
} // namespace cycle_sort
|
||||||
@ -84,11 +89,11 @@ std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
|||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
static void test() {
|
static void test() {
|
||||||
// [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362,
|
// Test 1
|
||||||
// 500, 506, 951]
|
// [4, 3, 2, 1] return [1, 2, 3, 4]
|
||||||
std::vector<int> array1 = {506, 48, 123, 79, 0, 362, 951, 500, 0};
|
std::vector<uint32_t> array1 = {4, 3, 2, 1};
|
||||||
std::cout << "Test 1... ";
|
std::cout << "Test 1... ";
|
||||||
std::vector<int> arr1 = sorting::cycle_sort::cycleSort(array1);
|
std::vector<uint32_t> arr1 = sorting::cycle_sort::cycleSort(array1);
|
||||||
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
|
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
|
||||||
std::cout << "passed" << std::endl;
|
std::cout << "passed" << std::endl;
|
||||||
|
|
||||||
@ -98,6 +103,21 @@ static void test() {
|
|||||||
std::vector<double> arr2 = sorting::cycle_sort::cycleSort(array2);
|
std::vector<double> arr2 = sorting::cycle_sort::cycleSort(array2);
|
||||||
assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
|
assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
|
||||||
std::cout << "passed" << std::endl;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user