Gnome sort
From Wikipedia, the free encyclopedia
| Class | Sorting algorithm |
|---|---|
| Data structure | Array |
| Worst case performance | O(n2) |
| Best case performance | O(n) |
| Worst case space complexity | O(1) auxiliary |
Gnome sort is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. The name comes from the supposed behavior of the Dutch garden gnome in sorting a line of flowerpots and is described on Dick Grune's Gnome sort page
Gnome Sort is based on the technique used by the standard Dutch Garden Gnome (Du.: tuinkabouter). Here is how a garden gnome sorts a line of flower pots. Basically, he looks at the flower pot next to him and the previous one; if they are in the right order he steps one pot forward, otherwise he swaps them and steps one pot backwards. Boundary conditions: if there is no previous pot, he steps forwards; if there is no pot next to him, he is done.
It is conceptually simple, requiring no nested loops. The running time is O(n²)[citation needed]. In practice the algorithm can run as fast as Insertion sort.
The algorithm always finds the first place where two adjacent elements are in the wrong order, and swaps them. It takes advantage of the fact that performing a swap can introduce a new out-of-order adjacent pair only right before or after the two swapped elements. It does not assume that elements forward of the current position are sorted, so it only needs to check the position directly before the swapped elements.
[edit] Description
Here is pseudocode for the sort. This is an optimized version which uses variable j to allow the gnome to skip back to where it left off after moving to the left, avoiding some iterations and comparisons:
function gnomeSort(a[0..size-1]) {
i := 1
j := 2
while i < size
if a[i-1] >= a[i] # for descending sort, reverse the comparison to <=
i := j
j := j + 1
else
swap a[i-1] and a[i]
i := i - 1
if i = 0
i := j
j := j + 1
}
Example:
If we wanted to sort an array with elements [4] [2] [7] [3] from highest to lowest, here is what would happen with each iteration of the while loop:
- [4] [2] [7] [3] (initial state. i is 1 and j is 2.)
- [4] [2] [7] [3] (did nothing, but now i is 2 and j is 3.)
- [4] [7] [2] [3] (swapped a[2] and a[1]. now i is 1 and j is still 3.)
- [7] [4] [2] [3] (swapped a[1] and a[0]. now i is 1 and j is still 3.)
- [7] [4] [2] [3] (did nothing, but now i is 3 and j is 4.)
- [7] [4] [3] [2] (swapped a[3] and a[2]. now i is 2 and j is 4.)
- [7] [4] [3] [2] (did nothing, but now i is 4 and j is 5.)
- at this point the loop ends because i isn't < 4.
[edit] Optimization
If optimized, the gnome sort will naturally transform into an insertion sort. Every time the "gnome" encounters a new number, all values to the left of the gnome are already sorted. The act of stepping backwards to place the number in its position corresponds to the act of shift-insertion into a list. If the gnome remembers is original position before going back to his original position, then the overall operation will correspond to a single insertion of insertion sort.
[edit] External links
| The Wikibook Algorithm implementation has a page on the topic of |
|
||||||||||||||||||||||||||||