Moves elements to correct position like a garden gnome sorting flower pots.
Gnome Sort is a simple sorting algorithm originally called "Stupid Sort" by its creator, Iranian computer scientist Hamid Sarbazi-Azad, who published it in 2000. It was later renamed "Gnome Sort" by Dutch computer scientist Dick Grune, who described the algorithm with a charming analogy: imagine a garden gnome sorting a line of flower pots. The gnome walks along the line, and whenever it finds two pots out of order, it swaps them and steps back to check again.
Sarbazi-Azad's original name, "Stupid Sort," reflected his view that the algorithm was remarkably unsophisticated. When Grune encountered it, he felt the algorithm deserved a friendlier name and introduced the garden gnome analogy that made the algorithm memorable and easy to teach. The name stuck, and Gnome Sort became a popular example in computer science education for illustrating how a simple set of rules can produce correct sorted output. It is sometimes cited as evidence that the simplest possible sorting logic, when followed consistently, will always work -- even if it is not efficient.
Imagine a gnome standing at the beginning of a line of flower pots, each with a number painted on it:
The beauty of Gnome Sort is that the gnome only needs to follow these simple rules. There are no nested loops, no index management beyond a single position variable, and no special logic for different cases. The entire algorithm can be expressed in pseudocode as a single while-loop with three conditions.
Gnome Sort is functionally equivalent to Insertion Sort but expressed differently. Both algorithms take an element that is out of place and move it backward through the sorted portion until it finds its correct position. The difference is in how they are written: Insertion Sort uses an explicit inner loop to shift elements and an outer loop to advance through the array, while Gnome Sort uses a single loop with a position variable that moves both forward and backward. When the gnome steps left (swapping as it goes), it is performing the same work as Insertion Sort's inner loop. When it steps right, it is advancing to the next unsorted element, like Insertion Sort's outer loop.
An optimization called "Gnome Sort with teleportation" saves the position the gnome was at before stepping left, so that after finishing the backward pass, the gnome can jump back to where it left off instead of walking forward through already-sorted elements. This optimization makes it identical to Insertion Sort in terms of comparisons and swaps.
| Case | Time | Space |
|---|---|---|
| Best | O(n) | O(1) |
| Average | O(n^2) | O(1) |
| Worst | O(n^2) | O(1) |
The best case of O(n) occurs when the array is already sorted and the gnome walks straight to the end without any backward steps. The worst case of O(n^2) occurs on reverse-sorted input, where every element must be carried all the way back to the beginning.
Gnome Sort is primarily of educational interest. Its value lies in its extreme simplicity: it demonstrates that correct sorting can emerge from a minimal set of rules, and it provides an intuitive physical analogy that helps beginners understand how comparison-based sorting works. For practical sorting, Insertion Sort is preferred because it achieves the same results with the same complexity but is typically implemented more efficiently. Gnome Sort is, however, an excellent choice for teaching, for code golf, or for situations where the simplest possible correct implementation is desired.