3
0
1
1
5
2
4
3
2
4
7
5
6
6
AlgoPlus//structures / cyclic-sort
Read the theory

Cyclic Sort

When values are 1..n, swap each one straight to its home index — no comparisons.

Stability
In-Place
Space Complexity
Avg Time
Your permutation of 1..n
Legend
Swapping home
In place
AI Tutor Workspace
In a nutshell
Cyclic sort works only when the values are a known range like 1 to n, where every value has one correct home: value v belongs at index v−1. It walks the array and, whenever a value is in the wrong spot, swaps it straight home. Because each swap puts at least one value where it belongs, the whole thing sorts in one sweep with no comparisons.
Ready
Press play to begin the cinematic walkthrough.
When values are 1..n, each belongs at a fixed index. Swap each value home in place — no comparisons, O(n).
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Cyclic Sort.