Gnome Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Gnome Sort
The garden gnome sort: a remarkably simple single-loop exchange sort modeled after a gnome checking adjacent flowerpots and moving backward or forward accordingly.
How it works
Gnome Sort (originally named "Stupid Sort" by Hamid Sarbazi-Azad in 2000, and later renamed by Dick Grune) is conceptually identical to Insertion Sort, but is implemented using only a single loop index. The namesake analogy describes a garden gnome sorting a line of flowerpots:
- Examine pots: The gnome starts at the first pot (\(i = 0\)). He looks at the current pot and the one before it.
- Step Forward: If they are in the correct order (or if the gnome is at the starting pot), he takes one step forward: \(i \leftarrow i + 1\).
- Swap and Step Backward: If the pots are in the wrong order, he swaps them and takes one step backward: \(i \leftarrow i - 1\).
- Termination: The gnome continues walking back and forth until he reaches the end of the line, guaranteeing the pots are fully sorted.
Complexity
Interactive visualization
Pseudocode
procedure gnomeSort(A): n ← length(A) i ← 0 while i < n: if i == 0 or A[i] ≥ A[i - 1]: i ← i + 1 else: swap A[i], A[i - 1] i ← i - 1
JavaScript implementation
function gnomeSort(arr) { const n = arr.length; let i = 0; while (i < n) { if (i === 0 || arr[i] >= arr[i - 1]) { i++; } else { [arr[i], arr[i - 1]] = [arr[i - 1], arr[i]]; i--; } } return arr; }
Pros and cons
Pros
- Extremely simple implementation, requiring only a single loop index.
- Fully stable: preserves relative ordering of equal keys.
- In-place: requires O(1) auxiliary space.
- Adaptive: linear time for sorted or nearly-sorted arrays.
Cons
- Average and worst-case complexities are quadratic \(O(n²)\).
- Performs a high number of comparison and swap cycles.
- Slower than Insertion Sort in practice because of the index decrement steps.
When (not) to use it
- Teaching logic: Excellent for illustrating how nested control loops can be converted into single index traversals.
- Practical sorting tasks: Do not use Gnome Sort for real-world workloads. Insertion Sort is always slightly faster, and Quicksort is orders of magnitude faster.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid