tap for sound

Gnome Sort Visualization

Gnome Sort steps forward while neighbours are in order and steps back, swapping, when they aren't.

How it works

Gnome Sort works like a garden gnome sorting flower pots. It looks at the pot in front of it: if it's in order with the previous one, it steps forward; if not, it swaps them and steps back. It keeps walking until it reaches the end.

It is effectively Insertion Sort done with swaps instead of shifts, so it is O(n²) on random data and close to linear on nearly sorted data. It is stable and uses a single loop with no nesting, which makes it one of the simplest sorts to write.

What to watch for

Watch the cursor run forward, then walk a value backwards step by step until it fits, then run forward again.

Complexity

Best
n
Average
n²
Worst
n²
Extra space
1
Stable
Yes

Race it

Same shuffle, same speed, one winner: Gnome vs Insertion. Or open the studio to try other layouts, sizes and speeds, and make a video of it.

FAQ

Is Gnome Sort the same as Insertion Sort?

Almost: it does the same moves, but swaps one step at a time instead of shifting, so it does more writes.

Is it stable?

Yes.