TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
cycle_sort.cpp File Reference

Implementation of Cycle sort algorithm. More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for cycle_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 
namespace  cycle_sort
 Functions for Cycle sort algorithm.
 

Functions

template<typename T >
std::vector< T > sorting::cycle_sort::cycleSort (const std::vector< T > &in_arr)
 The main function implements cycleSort.
 
static void test ()
 Test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of Cycle sort algorithm.

Cycle Sort is a sorting algorithm that works in \(O(n^2)\) time in the best case and works in \(O(n^2)\) 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

Definition in file cycle_sort.cpp.

Function Documentation

◆ cycleSort()

template<typename T >
std::vector< T > sorting::cycle_sort::cycleSort ( const std::vector< T > & in_arr)

The main function implements cycleSort.

Template Parameters
Ttype of array
Parameters
in_arrarray to be sorted
Returns
void

Definition at line 38 of file cycle_sort.cpp.

38 {
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}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 128 of file cycle_sort.cpp.

128 {
129 test(); // execute the test
130 return 0;
131}
static void test()
Test implementations.

◆ test()

static void test ( )
static

Test implementations.

Returns
void

Definition at line 92 of file cycle_sort.cpp.

92 {
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}
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.