# Cycle sort algorithm

In this article, we will discuss the Cycle sort Algorithm. Cycle sort is a comparison sorting algorithm that forces array to be factored into the number of cycles where each of them can be rotated to produce a sorted array. It is theoretically optimal in the sense that it reduces the number of writes to the original array.

It is an in-place and unstable sorting algorithm. Cycle sort forces an array to be factored into a number of cycles where every element can rotate in order to produce a sorted array. The time complexity of cycle sort is O(n2), even in the best case.

Now, let's see the algorithm of cycle sort.

## Algorithm

Suppose there is an array arr with n distinct elements. Given an element A, we can find its index by counting the number of elements smaller than A.

1. If the element is at its correct position, simply leave it as it is.
2. Else, we have to find the correct position of A by counting the number of elements smaller than it. Another element B is replaced to be moved to its correct position. This process continues until we get an element at the original position of A.

The above-illustrated process constitutes a cycle. Repeat this cycle for every element of the list until the list is sorted.

## Working of Cycle sort Algorithm

Now, let's see the working of Cycle sort Algorithm. To understand the working of cycle sort algorithm, let's take an unsorted array.

Let the elements of array are - {30, 20, 10, 40, 60, 50} Now, the given array is completely sorted.

## Cycle sort complexity

Now, let's see the time complexity of cycle sort in the best case, average case, and worst case. We will also see the space complexity of cycle sort.

### 1. Time Complexity

Case Time Complexity
Best Case O(2n)
Average Case O(2n)
Worst Case O(2n)

The time complexity of cycle sort in all three cases in O(n2). Even in the best case, it takes O(n2) time to sort the array elements. In Cycle sort, there is always the traversing of the entire subarray from the current position in order to count the number of elements less than the current element.

So, in cycle sort, it doesn't matter whether the given array is already sorted or not. It has no consequence on the running time of the algorithm. So, the cycle sort must run in the quadratic time.

### 2. Space Complexity

 Space Complexity O(1) Stable No
• The space complexity of cycle sort is O(1).

## Implementation of Cycle sort

Now, let's see the programs of cycle sort in different programming languages.

Program: Write a program to implement cycle sort in C language.

Output Program: Write a program to implement cycle sort in C++.

Output Program: Write a program to implement cycle sort in C#.

Output

After the execution of the above code, the output will be - Program: Write a program to implement cycle sort in Java.

Output

After the execution of the above code, the output will be - So, that's all about the article. Hope, the article will be helpful and informative to you.

Next TopicTim Sort

### Feedback   