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] 

Example

#include
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> n;
   int arr[n]; //create an array with given number of elements
   cout > arr[i];
   }

   cout 

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
Updated on: 2020-06-15T15:43:42+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements