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

Cycle sort is based on the idea that array to be sorted can be divided into cycles, which can individually be rotated to give a sorted result. More...

#include <iostream>
#include <vector>
Include dependency graph for cycle_sort2.cpp:

Namespaces

 sorting
 Sorting algorithms.
 
 cycle_sort
 Functions for Cycle sort algorithm.
 

Functions

template<typename T >
void sorting::cycle_sort::swap (T &a, T &b)
 Function to perform swapping. More...
 
template<typename T >
void sorting::cycle_sort::cycleSort (std::vector< T > &arr, int n)
 Function to perform cycle sort. More...
 
static void test ()
 Test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Cycle sort is based on the idea that array to be sorted can be divided into cycles, which can individually be rotated to give a sorted result.

It is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of memory writes to the original array. Each value is either written zero times, if it’s already in its correct position, or written one time to its correct position.

The array is divided into cycles. We begin with the cycle containing the first element. Find the correct position of first element and place it at its correct position, say j. Now we consider the old value of arr[j] and find its correct position. This process is continued until all the elements of the current cycle are placed at their correct position, i.e. until the starting point of the cycle is reached.

Author
Vasu Sehgal
See also
more on Cycle sort

Function Documentation

◆ cycleSort()

template<typename T >
void sorting::cycle_sort::cycleSort ( std::vector< T > &  arr,
int  n 
)

Function to perform cycle sort.

Parameters
arrvector of elements.
nsize of the vector
Returns
void
57  {
58 
59  int writes = 0; // count number of memory writes
60 
61  // traverse array elements and put it at it's correct positon
62 
63  for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) {
64 
65  T item = arr[cycle_start]; // initialize item as starting point
66 
67  int pos = cycle_start; // Find position where we put the item.
68  for (int i = cycle_start + 1; i < n; i++) // we basically count all smaller elements
69  if (arr[i] < item) // on the right side of the item.
70  pos++;
71 
72  if (pos == cycle_start)
73  continue; // If item is already in correct position
74 
75  while (item == arr[pos])
76  pos += 1; // ignore all duplicate elements
77 
78 
79  if (pos != cycle_start) {
80  swap(item, arr[pos]); // place the item at it's correct position
81  writes++;
82  }
83 
84  while (pos != cycle_start) { // Rotate rest of the cycle
85  pos = cycle_start;
86 
87  for (int i = cycle_start + 1; i < n; i++) // Find position where we put the element
88  if (arr[i] < item)
89  pos += 1;
90 
91  while (item == arr[pos])
92  pos += 1; // ignore all duplicate elements
93 
94 
95  if (item != arr[pos]) {
96  swap(item, arr[pos]); // place the item at it's correct position
97  writes++;
98  }
99  }
100  }
101 
102  // Number of memory writes or swaps
103  // cout << writes << endl ;
104  }
T swap(T... args)
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
142 {
143  test(); // execute the tests
144  return 0;
145 }
static void test()
Test implementations.
Definition: cycle_sort2.cpp:114
Here is the call graph for this function:

◆ swap()

template<typename T >
void sorting::cycle_sort::swap ( T &  a,
T &  b 
)

Function to perform swapping.

Parameters
aelement
belement
Returns
void
43  {
44  T temp = a;
45  a = b;
46  b = temp;
47  }

◆ test()

static void test ( )
static

Test implementations.

Returns
void
114  {
115 
116  // example 1: vector of int
117  const int size1 = 8;
118  std::cout << "Test 1: After sort-\n";
119  std::vector<int> arr1 = { 1, 8, 3, 9, 10, 10, 2, 4 };
120  sorting::cycle_sort::cycleSort(arr1, size1);
121  for (int i = 0; i < size1; i++)
122  std::cout << arr1[i] << " ";
123  std::cout<<"\n";
124 
125  // example 2: vector of double
126  const int size2 = 8;
127  std::cout << "Test 2: After sort-\n";
128  std::vector<double> arr2 = {11.82, 45.10, 33.22, 33.18, 1.9, 1.2, 61.77, 79.6};
129  sorting::cycle_sort::cycleSort(arr2, size2);
130 
131  for (int i = 0; i < size2; i++)
132  std::cout << arr2[i] << " ";
133  std::cout<<"\n";
134 
135 
136 }
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:39