Cycle Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Cycle Sort
An unstable, in-place comparison sort that is theoretically optimal in terms of the total number of array writes: places every element in its final sorted slot by decomposing the array permutation into cycles.
How it works
Cycle Sort is unique because it minimizes array write operations. In systems where writing to memory is highly expensive (e.g. Flash EEPROM memory where write cycles wear out the hardware), Cycle Sort is highly valued because each element is written **either 0 or 1 time** to its final position. The algorithm decomposes the array's permutation into cyclic shifts:
- Find correct position: For each index
cycleStart, take the element at that index (call ititem). Search the rest of the array to count how many elements are smaller thanitem. This count gives its correct sorted index position,pos. - Perform write: If
posmatchescycleStart, the element is already in place. Otherwise, swapitemwith the element atpos. This puts the original item in its final sorted position. - Rotate cycle: The swapped-out element is now the new active
item. Find its correct position and swap again. Repeat this cyclic routing until we write an element back into the starting indexcycleStart, completing the cycle.
Complexity
Interactive visualization
Pseudocode
procedure cycleSort(A): n ← length(A) for cycleStart from 0 to n - 2: item ← A[cycleStart] pos ← cycleStart for i from cycleStart + 1 to n - 1: if A[i] < item: pos ← pos + 1 if pos == cycleStart: continue while item == A[pos]: pos ← pos + 1 swap item, A[pos] # item now holds the displaced value while pos ≠ cycleStart: pos ← cycleStart for i from cycleStart + 1 to n - 1: if A[i] < item: pos ← pos + 1 while item == A[pos]: pos ← pos + 1 swap item, A[pos]
JavaScript implementation
function cycleSort(arr) { const n = arr.length; for (let cycleStart = 0; cycleStart < n - 1; cycleStart++) { let item = arr[cycleStart]; let pos = cycleStart; for (let i = cycleStart + 1; i < n; i++) { if (arr[i] < item) pos++; } if (pos === cycleStart) continue; while (item === arr[pos]) pos++; if (pos !== cycleStart) { let temp = arr[pos]; arr[pos] = item; item = temp; } while (pos !== cycleStart) { pos = cycleStart; for (let i = cycleStart + 1; i < n; i++) { if (arr[i] < item) pos++; } while (item === arr[pos]) pos++; if (item !== arr[pos]) { let temp = arr[pos]; arr[pos] = item; item = temp; } } } return arr; }
Pros and cons
Pros
- Optimal number of memory writes: exactly 0 or 1 write per element.
- Highly effective for Flash EEPROM or NVRAM where writes cause wear.
- Fully in-place: uses O(1) auxiliary space.
Cons
- Quadratic time complexity \(O(n²)\) in best, average, and worst-case.
- Unstable sorting order.
- Highly inefficient on standard DRAM architectures where reads are as slow as writes.
When (not) to use it
- Flash/EEPROM storage: Highly recommended when writing to memory introduces wear or high latency, such as in embedded EEPROM devices.
- Standard computer systems: Avoid Cycle Sort. On general systems, Quicksort, Heapsort, or Mergesort are significantly faster because they have \(O(n \log n)\) time complexity.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid