Gnome Sort
- Ω( n )
- Θ( n2 )
- O( n2 )
- space O( 1 )
Gnome sort has one index and no nested loops. The index advances while the element under it is not smaller than its left neighbour. When it is smaller, the two swap and the index steps back one cell, then checks again. Walking backwards after a swap is what carries a small value all the way to its place, which makes the method insertion sort carried out with swaps.
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How gnome sort works
The gnome starts at index 0, which has no left neighbour, so it steps forward. At index 1 the 2 is smaller than the 5 before it, so they swap into [2, 5, 6, 1, 4, 3] and the gnome steps back to index 0. From there it walks forward over 5 and 6 without any swap, since each is at least as large as the one before it, until it reaches the 1 at index 3.
The 1 now has to travel to the front, and the gnome carries it there one swap at a time. The array goes through [2, 5, 1, 6, 4, 3], then [2, 1, 5, 6, 4, 3] and then [1, 2, 5, 6, 4, 3], and the index ends up back at 0. The gnome walks forward again over the sorted prefix to the 4 at index 4. That value swaps with the 6, then with the 5, giving [1, 2, 5, 4, 6, 3] and [1, 2, 4, 5, 6, 3], and it stops when 2 is not larger than 4.
The last value, 3, makes the longest trip of all. It swaps three times to give [1, 2, 4, 5, 3, 6], then [1, 2, 4, 3, 5, 6], and finally [1, 2, 3, 4, 5, 6]. After that the gnome walks off the right end. The run uses 9 swaps, one for each inversion, and 21 comparisons. That is roughly two comparisons per swap, because after each insertion the gnome walks forward over cells it has just passed.
| Step | Change | Array after |
|---|---|---|
| 1 | Swap indexes 1 and 0 | 2 5 6 1 4 3 |
| 2 | Swap indexes 3 and 2 | 2 5 1 6 4 3 |
| 3 | Swap indexes 2 and 1 | 2 1 5 6 4 3 |
| 4 | Swap indexes 1 and 0 | 1 2 5 6 4 3 |
| 5 | Swap indexes 4 and 3 | 1 2 5 4 6 3 |
| 6 | Swap indexes 3 and 2 | 1 2 4 5 6 3 |
| 7 | Swap indexes 5 and 4 | 1 2 4 5 3 6 |
| 8 | Swap indexes 4 and 3 | 1 2 4 3 5 6 |
| 9 | Swap indexes 3 and 2 | 1 2 3 4 5 6 |
All 9 changes are shown.
Pseudocode
i = 0
while i < n
if i == 0 or a[i - 1] <= a[i]
i = i + 1
else
swap a[i], a[i - 1]
i = i - 1
Why these complexities
On sorted input the gnome never swaps. It steps off index 0 for free, then makes one comparison per cell, n-1 in total, and walks off the end. That is the linear best case, and it is why gnome sort is adaptive: the work follows the number of inversions, and a sorted array has none.
Every swap removes one inversion, so the swap count is always exactly the number of inversions: 0 for sorted data, about n(n-1)/4 for a random permutation and n(n-1)/2 for a reversed one. Comparisons are the part that depends on the walk. Each swap is itself a comparison, and each step back is followed later by a step forward over the same cell, which is another comparison. The total is about twice the swaps, so a reversed array costs close to n² comparisons. All of this is O(n²).
Space is constant. The algorithm keeps one index and nothing else, and every move happens inside the array. Stability holds because the gnome steps back only while the element is strictly smaller than the one on its left. An equal key makes the gnome stop, so two equal values never cross.
Measured on this site
Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.
| Input | Comparisons | Writes | Swaps |
|---|---|---|---|
| Random | 649 | 0 | 311 |
| Already sorted | 31 | 0 | 0 |
| Reversed | 992 | 0 | 496 |
| Few distinct values | 369 | 0 | 169 |
When to use it
Use it to show how a sort can be written with one index and no nested loops. For real data, insertion sort does the same job with fewer writes.
Frequently asked questions
How does gnome sort relate to insertion sort?
It does the same work. Both grow a sorted prefix, but gnome sort moves an element into place with repeated swaps instead of shifting.
Is gnome sort stable?
Yes. It stops stepping back as soon as the element is not smaller than the one before it.
What is its complexity?
O(n²) comparisons on average and in the worst case, and O(n) on sorted input.
How many comparisons does gnome sort make compared with insertion sort?
About twice as many on unsorted data. Insertion sort compares an element against the sorted prefix once per shifted position. Gnome sort compares during the walk back and then again while walking forward over the cells it just crossed. On the sample array that is 21 comparisons for gnome sort, against fewer than 15 for a plain insertion sort that stops at the first element not larger than the key.
Why is the index not compared every time it moves?
At index 0 there is no left neighbour, so the code steps forward without a comparison. The check for index 0 comes first, so the comparison is never made and is not counted. In the trace the index returns to 0 twice and starts there once, so three of the 15 forward steps cost nothing. The other 12 forward steps and the 9 swaps account for all 21 comparisons.
Background
The method is usually credited to Hamid Sarbazi-Azad, who proposed it in 2000 under the name stupid sort. Dick Grune later renamed it gnome sort and gave it the garden-gnome story: a gnome sorting a row of flower pots looks at the pot beside him, and if the two are in order he steps forward, and otherwise swaps them and steps back.
It has never competed with real algorithms. Its value is that the whole procedure fits in a single loop with no nested structure, which makes it a handy way to show that insertion sort can be written without an inner loop, and what is lost when it is: the repeated forward walk.
References
- Knuth, The Art of Computer Programming, vol. 3, section 5.2.1 (Sorting by Insertion)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, section 2.1 (Insertion sort)
- Knuth, The Art of Computer Programming, vol. 3, section 5.1.1 (Inversions)
Compare with
| Algorithm | Average time | Worst time |
|---|---|---|
| Bubble Sort | Θ( n2 ) | O( n2 ) |
| Cocktail Shaker Sort | Θ( n2 ) | O( n2 ) |
| Comb Sort | Θ( n2 ) | O( n2 ) |
| Odd-Even Sort | Θ( n2 ) | O( n2 ) |
| Quick Sort | Θ( n log n ) | O( n2 ) |
| 3-Way Quick Sort | Θ( n log n ) | O( n2 ) |
| Dual-Pivot Quick Sort | Θ( n log n ) | O( n2 ) |
| Best time | Ω( n ) | good |
|---|---|---|
| Average time | Θ( n2 ) | poor |
| Worst time | O( n2 ) | poor |
| Worst space | O( 1 ) | good |
Properties
- Stable
- Yes
- In place
- Yes
- Adaptive
- Yes
- Comparison-based
- Yes