Skip to content
StackDecode
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

  1. Cycle sort minimises the number of writes to memory, which matters for flash storage.
  2. Take the item at a position. Count how many items are smaller: that count is where it belongs.
  3. Put it there, pick up the item it displaced, and repeat until the cycle returns to the start.
  4. Move on to the next position. Each item is written at most once.

Watch it run

Loading animation…