Cycle Sort


Cycle Sort is an in-place sorting algorithm. It is also a comparison based sort and efficient for any other in-place sorting technique. It finds the minimum number of memory write to perform the sorting tasks.

The complexity of Cycle Sort Technique

  • Time Complexity: O(n^2)
  • Space Complexity: O(1)

Input and Output

Input:
A list of unsorted data: 23 63 98 74 20 14 36 45 99 78
Output:
Array before Sorting: 23 63 98 74 20 14 36 45 99 78
Array after Sorting: 14 20 23 36 45 63 74 78 98 99

Algorithm

cycleSort(array, size)

Input − An array of data, and the total number in the array

Output − The sorted Array

Begin
   for start := 0 to n – 2 do
      key := array[start]
      location := start
      for i := start + 1 to n-1 do
         if array[i] < key then
            location:=location +1
      done

      if location = start then
         ignore lower part, go for next iteration
      while key = array[location] do
         location := location +1
      done

      if location ≠ start then
         swap array[location] with key
      while location ≠ start do
         location := start
         for i := start + 1 to n-1 do
            if array[i] < key then
               location:=location +1
         done

         while key = array[location]
            location := location +1
         if key ≠ array[location]
            swap array[location] and key
      done
   done
End

Example

#include<iostream>
using namespace std;

void swapping(int &a, int &b) {     //swap the content of a and b
   int temp;
   temp = a;
   a = b;
   b = temp;
}

void display(int *array, int size) {
   for(int i = 0; i<size; i++)
      cout << array[i] << " ";
   cout << endl;
}

void cycleSort(int *array, int n) {
   for(int start = 0; start<n-1; start++) {    //put array element in the correct place
      int key = array[start];
      int location = start;

      for(int i = start+1; i<n; i++) { //count smaller element in the right side of key
         if(array[i] < key)
            location++;
      }

      if(location == start) //when it is in correct place go for next iteration
         continue;
      while(key == array[location]) //if same data is found increase location
         location++;
      if(location != start)
         swapping(array[location], key);

      while(location != start) {
         location = start;

         for(int i = start+1; i<n; i++) { //location to put element
            if(array[i] < key)
               location++;
         }

         while(key == array[location]) //if same data is found increase location
            location++;
         if(key != array[location])
            swapping(key, array[location]);
      }
   }
}

int main() {
   int n;
   cout << "Enter the number of elements: ";
   cin >> n;
   int arr[n]; //create an array with given number of elements
   cout << "Enter elements:" << endl;

   for(int i = 0; i<n; i++) {
      cin >> arr[i];
   }

   cout << "Array before Sorting: ";
   display(arr, n);
   cycleSort(arr, n);
   cout << "Array after Sorting: ";
   display(arr, n);
}

Output

Enter the number of elements: 10
Enter elements:
23 63 98 74 20 14 36 45 99 78
Array before Sorting: 23 63 98 74 20 14 36 45 99 78
Array after Sorting: 14 20 23 36 45 63 74 78 98 99

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 15-Jun-2020

578 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements