Gnome sort
Gnome sort is a variation of the insertion sort sorting algorithm that does not use nested loops. Gnome sort was known for a long time and used without naming it explicitly. It was then popularized by Iranian computer scientist Hamid Sarbazi-Azad in 2000. The sort was first called stupid sort, and then later described by Dick Grune and named gnome sort.
Gnome sort performs at least as many comparisons as insertion sort and has the same asymptotic run time characteristics. Gnome sort works by building a sorted list one element at a time, getting each item to the proper place in a series of swaps. The average running time is O but tends towards O if the list is initially almost sorted.
Dick Grune described the sorting method with the following story:
Pseudocode
Here is pseudocode for the gnome sort using a zero-based array:procedure gnomeSort:
pos := 1
while pos < length:
if :
pos := pos + 1
else:
swap a and a
pos := pos - 1
Example
Given an unsorted array, a =, the gnome sort takes the following steps during the while loop. The current position is highlighted in bold and indicated as a value of the variablepos.| Current array | pos | Condition in effect | Action to take |
| 0 | pos 0 | increment pos | |
| 1 | a < a | swap, decrement pos | |
| 0 | pos 0 | increment pos | |
| 1 | a ≥ a | increment pos | |
| 2 | a < a | swap, decrement pos | |
| 1 | a < a | swap, decrement pos | |
| 0 | pos 0 | increment pos | |
| 1 | a ≥ a | increment pos | |
| 2 | a ≥ a | increment pos: | |
| 3 | a < a | swap, decrement pos | |
| 2 | a ≥ a | increment pos | |
| 3 | a ≥ a | increment pos | |
| 4 | pos length | finished |