Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
cycle_sort.cpp File Reference

Implementation of Cycle sort algorithm. More...

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

Namespaces

 sorting
 Sorting algorithms.
 
 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. More...
 
static void test ()
 Test implementations. More...
 
int main ()
 Main function. More...
 

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
37  {
38  std::vector<T> arr(in_arr);
39  for (int cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
40  // initialize item
41  T item = arr[cycle_start];
42 
43  // Count the number of elements smaller than item, this number is the
44  // correct index of item.
45  int pos = cycle_start;
46  for (int i = cycle_start + 1; i < arr.size(); i++) {
47  if (arr[i] < item) {
48  pos++;
49  }
50  }
51 
52  // item is already in correct position
53  if (pos == cycle_start) {
54  continue;
55  }
56 
57  // duplicate elements
58  while (item == arr[pos]) pos += 1;
59  if (pos == cycle_start) {
60  continue;
61  } else {
62  std::swap(item, arr[pos]);
63  }
64  // Rest of the elements
65  while (pos != cycle_start) {
66  pos = cycle_start;
67  // Find position where we put the element
68  for (size_t i = cycle_start + 1; i < arr.size(); i++) {
69  if (arr[i] < item) {
70  pos += 1;
71  }
72  }
73  // duplicate elements
74  while (item == arr[pos]) pos += 1;
75  if (item == arr[pos]) {
76  continue;
77  } else {
78  std::swap(item, arr[pos]);
79  }
80  }
81  }
82  return arr;
83 }
T swap(T... args)
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

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

◆ test()

static void test ( )
static

Test implementations.

Returns
void
91  {
92  // Test 1
93  // [4, 3, 2, 1] return [1, 2, 3, 4]
94  std::vector<uint32_t> array1 = {4, 3, 2, 1};
95  std::cout << "Test 1... ";
97  assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
98  std::cout << "passed" << std::endl;
99 
100  // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
101  std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
102  std::cout << "Test 2... ";
104  assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
105  std::cout << "passed" << std::endl;
106 
107  // Test 3
108  // [3, 3, 3, 3] return [3, 3, 3, 3]
109  std::vector<uint32_t> array3 = {3, 3, 3, 3};
110  std::cout << "Test 3... ";
112  assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
113  std::cout << "passed" << std::endl;
114 
115  // [9, 4, 6, 8, 14, 3] return [9, 4, 6, 8, 14, 3]
116  std::vector<uint32_t> array4 = {3, 4, 6, 8, 9, 14};
117  std::cout << "Test 4... ";
119  assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
120  std::cout << "passed" << std::endl;
121 }
T begin(T... args)
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:37
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
Here is the call graph for this function: