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

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
107  {
108  test(); // execute the test
109  return 0;
110 }
static void test()
Test implementations.
Definition: cycle_sort.cpp:86
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
86  {
87  // [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362,
88  // 500, 506, 951]
89  std::vector<int> array1 = {506, 48, 123, 79, 0, 362, 951, 500, 0};
90  std::cout << "Test 1... ";
92  assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
93  std::cout << "passed" << std::endl;
94 
95  // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
96  std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
97  std::cout << "Test 2... ";
99  assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
100  std::cout << "passed" << std::endl;
101 }
T begin(T... args)
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:39
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
Here is the call graph for this function: