Gnome Sort - Optimization

Optimization

The gnome sort may be optimized by introducing a variable to store the position before traversing back toward the beginning of the list. This would allow the "gnome" to teleport back to his previous position after moving a flower pot. With this optimization, the gnome sort would become a variant of the insertion sort.

Here is pseudocode for an optimized gnome sort using a zero-based array:

procedure optimizedGnomeSort(a) pos := 1 last := 0 while pos < length(a) if (a >= a) if (last != 0) pos := last last := 0 end if pos := pos + 1 else swap a and a if (pos > 1) if (last == 0) last := pos end if pos := pos - 1 else pos := pos + 1 end if end if end while end procedure

Read more about this topic:  Gnome Sort