Back to Articles
Algorithms7 min read

Gnome Sort / "Stupid" Sort

A look at the deceptively simple Gnome Sort: how it works, why it is slow, and why it is still worth studying.

By Thai Nguyen

North Garland H.S. Chapter

What is it

Although it is sometimes referred to as the "stupid sort," the Gnome sort is a legitimate comparison-based sorting algorithm. In essence, the sort is very simplistic: a given list is sequenced through, one element at a time. If the current element is less than the last element, they swap positions.

History

Before it was known as the Gnome Sort, Iranian computer scientist Hamid Sarbazi-Azad originally described it as "Stupid Sort" in a 2000 university newsletter. Due to discontent with its academic wording, Dutch computer scientist Dick Grune later renamed it based on a Dutch garden gnome (tuinkabouter) sorting flower pots. In his visualization, the gnome only looks at 2 "pots." If they are in order, he steps forward; if not, he steps back and re-evaluates the previous pots.

Problem in Implementation

Unlike the more commonly used Bubble or Insertion Sort, Gnome Sort can be implemented in a single while loop and no nested structures. While it does lack the nested loop found in more popular sorting algorithms, its time complexity — a mathematical way to describe how much time an algorithm takes to run as the size of its input increases — of O(n²) is incredibly abysmal. This is caused by the fact that the "gnome" must constantly move backwards in large unordered lists.

procedure gnomeSort(a[]):
    pos := 0
    while pos < length(a):
        if pos == 0 or a[pos] >= a[pos-1]:
            pos := pos + 1
        else:
            swap a[pos] and a[pos-1]
            pos := pos - 1

Despite the speed flaws that it has, the Gnome Sort is very space-efficient, being classified as an O(1) auxiliary space algorithm. Gnome Sort utilizes an in-place system, meaning it rearranges the elements directly within the original array, not needing to create a "working" array to hold sorted data (for example, Merge Sort). In addition, the auxiliary space used by Gnome Sort is constant because it uses minimal variables, only needing:

  • One index variable (usually called pos or i) to track the gnome's position
  • One temporary variable used to swap elements

This distinct advantage is, of course, counteracted by the time needed per operation. However, this still makes it attractive to environments where memory / RAM is a big limiting factor.

def gnome_sort(arr):
    index = 0
    while index < len(arr):
        if index == 0:
            index = index + 1
        if arr[index] >= arr[index - 1]:
            index = index + 1
        else:
            # The swap: moving the "pot" back one step
            arr[index], arr[index - 1] = arr[index - 1], arr[index]
            index = index - 1
    return arr

# Example usage:
my_list = [34, 2, 10, -9]
print(gnome_sort(my_list))

Comparing Performance

In many studies, it is referred to as a hybrid that behaves similar to an Insertion Sort but uses the pairwise swap mechanism of the Bubble Sort. The Gnome Sort sees its most performative usage when the list is already sorted, allowing the "gnome" to simply walk all the way to the end without ever stepping back.

In a reverse-sorted list, however, the Gnome Sort really shows why it's called the "Stupid Sort." This is the worst possible scenario as the number of swaps is maximized. Empirical studies show that Bubble Sort actually outperforms Gnome Sort as data size increases in worst-case scenarios.

Why Study It Then?

This sorting algorithm is a prime example of a pathological algorithm — one that is logically simple but computationally expensive. While it is absolutely useless for large datasets, it is a useful teaching tool for understanding the way index movement affects algorithms, serving as a baseline that often leads to other, more effective algorithms.