Cycle sort reads the mapping from current positions to sorted positions as a set of cycles. At cycleStart, it counts smaller values to find where the current value belongs. It writes the value straight there, picks up the displaced value, and follows the cycle until it returns to cycleStart.

With distinct values, each displaced element goes to its final position in one write and never moves again. This is the minimum number of writes for a comparison sort that reuses the input array. The tradeoff is repeated scanning: every carried value needs another count over the remaining suffix. Duplicate values also require skipping equal destinations, or the cycle may 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

Write minimization and duplicate keys

Cycle sort is unstable because skipping past equal destinations does not preserve the input order of equal records. Its minimum-write property is cleanest with distinct keys, where every displaced element needs one final-position write. The algorithm fits the narrow case where writes cost far more than reads. Most production sorts avoid its repeated scans.

References