sort.cny.shStart a race
Back to library
05 / 18Algorithm

Gnome Sort

A gnome walks the row, swapping backward until it fits.

StableIn-place
BestO(n)
AverageO(n²)
WorstO(n²)
01How it works

Walk forward; if the current pair is in order, step right; otherwise swap and step left. Unsorted elements are carried back one slot at a time — same effect as insertion sort, but with one index and one loop.

02Best for

Teaching and code-golf — very few lines of logic, no nested loops, trivial to verify by hand. Occasionally appears in size-constrained embedded contexts for the same reason.

03Avoid when

Performance matters. Same O(n²) cost as insertion sort with extra pointer bouncing — it's a curiosity more than a tool.