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

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
101  {
102  test(); // execute the test
103  return 0;
104 }
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
81  {
82  // [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362, 500, 506, 951]
83  std::vector<int> array1 = {506, 48, 123, 79, 0, 362, 951, 500, 0};
84  std::cout << "Test 1... ";
86  assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
87  std::cout << "passed" << std::endl;
88 
89  // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
90  std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
91  std::cout << "Test 2... ";
93  assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
94  std::cout << "passed" << std::endl;
95 }
Here is the call graph for this function:
std::vector
STL class.
std::is_sorted
T is_sorted(T... args)
std::cout
test
static void test()
Test implementations.
Definition: cycle_sort.cpp:81
sorting::cycle_sort::cycleSort
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:35
std::swap
T swap(T... args)
std::endl
T endl(T... args)
std::begin
T begin(T... args)
std::end
T end(T... args)