SortingAnimated
Cycle Sort
Rotate items into place, writing each at most once.
Time
O(n²)
Extra space
O(1)
Use it for
Minimising writes (flash memory).
How it works
- Cycle sort minimises the number of writes to memory, which matters for flash storage.
- Take the item at a position. Count how many items are smaller: that count is where it belongs.
- Put it there, pick up the item it displaced, and repeat until the cycle returns to the start.
- Move on to the next position. Each item is written at most once.
Watch it run
Loading animation…