Cycle sort is an in-place sorting Algorithm, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array.

  • It is optimal in terms of number of memory writes. It minimizes the number of memory writes to sort (Each value is either written zero times, if it’s already in its correct position, or written one time to its correct position.)
  • It is based on the idea that array to be sorted can be divided into cycles. Cycles can be visualized as a graph. We have n nodes and an edge directed from node i to node j if the element at i-th index must be present at j-th index in the sorted array.
    Cycle in arr[] = {4, 5, 2, 1, 5}
  • We one by one consider all cycles. We first consider the cycle that includes first element. We find correct position of first element, place it at its correct position, say j. We consider old value of arr[j] and find its correct position, we keep doing this till all elements of current cycle are placed at correct position, i.e., we don’t come back to cycle starting point.

    Recommended: Please try your approach on {IDE} first, before moving on to the solution.

    Explanation :

     arr[] = {10, 5, 2, 3}
     index =  0   1   2   3
    cycle_start = 0 
    item = 10 = arr[0]
    
    Find position where we put the item  
    pos = cycle_start
    while (arr[i] < item)  
        pos++;
    
    We put 10 at arr[3] and change item to 
    old value of arr[3].
    arr[] = {10, 5, 2, 10} 
    item = 3 
    
    Again rotate rest cycle that start with index '0' 
    Find position where we put the item = 3 
    we swap item with element at arr[1] now 
    arr[] = {10, 3, 2, 10} 
    item = 5
    
    Again rotate rest cycle that start with index '0' and item = 5 
    we swap item with element at arr[2].
    arr[] = {10, 3, 5, 10 } 
    item = 2
    
    Again rotate rest cycle that start with index '0' and item = 2
    arr[] = {2 ,3 , 5, 10}  
    
    Above is one iteration for cycle_stat = 0.
    Repeat above steps for cycle_start = 1, 2, ..n-2

     

    [ad type=”banner”]
  • Below is C++ implementation of above Cycle Sort.
[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20impleament%20cycle%20sort%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Function%20sort%20the%20array%20using%20Cycle%20sort%0Avoid%20cycleSort%20(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20count%20number%20of%20memory%20writes%0A%20%20%20%20int%20writes%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20traverse%20array%20elements%20and%20put%20it%20to%20on%0A%20%20%20%20%2F%2F%20the%20right%20place%0A%20%20%20%20for%20(int%20cycle_start%3D0%3B%20cycle_start%3C%3Dn-2%3B%20cycle_start%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20initialize%20item%20as%20starting%20point%0A%20%20%20%20%20%20%20%20int%20item%20%3D%20arr%5Bcycle_start%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20position%20where%20we%20put%20the%20item.%20We%20basically%0A%20%20%20%20%20%20%20%20%2F%2F%20count%20all%20smaller%20elements%20on%20right%20side%20of%20item.%0A%20%20%20%20%20%20%20%20int%20pos%20%3D%20cycle_start%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%20cycle_start%2B1%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3C%20item)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pos%2B%2B%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20item%20is%20already%20in%20correct%20position%0A%20%20%20%20%20%20%20%20if%20(pos%20%3D%3D%20cycle_start)%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20ignore%20all%20duplicate%20%20elements%0A%20%20%20%20%20%20%20%20while%20(item%20%3D%3D%20arr%5Bpos%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20pos%20%2B%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20put%20the%20item%20to%20it’s%20right%20position%0A%20%20%20%20%20%20%20%20if%20(pos%20!%3D%20cycle_start)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20swap(item%2C%20arr%5Bpos%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20writes%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Rotate%20rest%20of%20the%20cycle%0A%20%20%20%20%20%20%20%20while%20(pos%20!%3D%20cycle_start)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20pos%20%3D%20cycle_start%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20position%20where%20we%20put%20the%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%20cycle_start%2B1%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3C%20item)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pos%20%2B%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20ignore%20all%20duplicate%20%20elements%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(item%20%3D%3D%20arr%5Bpos%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pos%20%2B%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20put%20the%20item%20to%20it’s%20right%20position%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(item%20!%3D%20arr%5Bpos%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20swap(item%2C%20arr%5Bpos%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20writes%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Number%20of%20memory%20writes%20or%20swaps%0A%20%20%20%20%2F%2F%20cout%20%3C%3C%20writes%20%3C%3C%20endl%20%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%208%2C%203%2C%209%2C%2010%2C%2010%2C%202%2C%204%20%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20cycleSort(arr%2C%20%20n)%20%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22After%20sort%20%3A%20%22%20%3C%3Cendl%3B%0A%20%20%20%20for%20(int%20i%20%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

utput:

1 3 4 8 10 10 

Time Complexity : O(n2)
Worst Case : O(n2)
Average Case: O(n2)
Best Case : O(n2)

This sorting algorithm is best suited for situations where memory write or swap operations are costly.

[ad type=”banner”]