Python: Sort a list of elements using Cycle sort
16. Cycle Sort
Write a Python program to sort a list of elements using Cycle sort.
Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.
Sample Solution:
Python Code:
Sample Output:
[0, 1, 2, 2, 2, 2, 1, 9, 3.5, 5, 8, 4, 7, 0, 6] Is correctly sorted using cycleSort to [0, 0, 1, 1, 2, 2, 2, 2, 3.5, 4, 5, 6, 7, 8, 9] Using 10 writes.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to implement cycle sort and print the number of writes made to sort the array.
- Write a Python script to use cycle sort on a list of unique integers and then verify that the algorithm minimizes data movement.
- Write a Python program to implement cycle sort and output the cycles detected during the sorting process.
- Write a Python function to perform cycle sort on a list and then compare the result with Python’s built-in sorted() function.
Go to:
Previous: Write a Python program to sort a list of elements using Comb sort.
Next: Write a Python program to sort a list of elements using Heap sort.
Python Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.