Algorithms_in_C++ 1.0.0
Set of 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:

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

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
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}
T swap(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
128 {
129 test(); // execute the test
130 return 0;
131}
static void test()
Test implementations.
Definition cycle_sort.cpp:92
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
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... ";
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... ";
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... ";
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... ";
120 assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
121 std::cout << "passed" << std::endl;
122}
T begin(T... args)
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition cycle_sort.cpp:38
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
Here is the call graph for this function: