Cycle sort treats the mapping from current positions to sorted positions as a set of cycles. For the value at cycleStart, it counts smaller values to find the final position, writes the value there, and carries the displaced value to its own final position. The rotation ends when the displaced chain returns to cycleStart.

For distinct values, every displaced element is written directly to its final position and never moved again. That minimizes writes among comparison sorts that reuse the input array, while repeatedly counting smaller suffix values still revisits the remainder for each cycle. Duplicate values require advancing past equal values so a cycle does not keep exchanging indistinguishable entries.

Visualization

The trace separates comparisons from writes. A scan computes the rank of the carried value, then one overwrite places it and picks up the displaced value. Equal values are skipped when selecting the destination.

Complexity
n
number of elements in the array

Boundary and implementation

Cycle sort is unstable: placing a duplicate after equal destinations does not preserve the original order among equal records. The minimum-write argument is strongest for distinct keys, where each displaced element needs one final-position write. It is a niche choice when writes are substantially more expensive than reads; production sorts usually prefer fewer repeated scans.

Questions

References