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

  T | type of array

Parameters

  in_arr | array to be sorted

Returns
  void

{
std::vector<T> arr(in_arr);
for (size_t cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
// initialize item
T item = arr[cycle_start];
// Count the number of elements smaller than item, this number is the
// correct index of item.
int pos = cycle_start;
for (size_t i = cycle_start + 1; i < arr.size(); i++) {
if (arr[i] < item) {
pos++;
}
}
// item is already in correct position
if (pos == cycle_start) {
continue;
}
// duplicate elements
while (item == arr[pos]) pos += 1;
std::swap(item, arr[pos]);
// Rest of the elements
while (pos != cycle_start) {
pos = cycle_start;
// Find position where we put the element
for (size_t i = cycle_start + 1; i < arr.size(); i++) {
if (arr[i] < item) {
pos += 1;
}
}
// duplicate elements
while (item == arr[pos]) pos += 1;
std::swap(item, arr[pos]);
}
}
return arr;
}
std::swap
T swap(T... args)
std::vector
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // execute the test
return 0;
}
test
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

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