Skip to content

Gnome Sort

A simple sorting algorithm that moves elements backward when out of order, similar to insertion sort with swaps.

Gnome sort is a simple comparison-based sorting algorithm. It scans forward through the array, and when it finds two adjacent elements out of order, it swaps them and steps backward. When elements are in order, it moves forward.

The behavior resembles insertion sort, but instead of shifting elements, it repeatedly swaps while moving backward.

Problem

Given a sequence AA of length nn, reorder it such that

A[0]A[1]A[n1] A[0] \le A[1] \le \cdots \le A[n-1]

Algorithm

Maintain an index that moves forward when order is correct and backward after swaps.

gnome_sort(A):
    i = 0
    n = length(A)

    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

Example

Let

A=[5,3,2,4] A = [5, 3, 2, 4]

Steps:

stepindexactionresult
11swap (5,3)[3,5,2,4]
21keep[3,5,2,4]
32swap (5,2)[3,2,5,4]
41swap (3,2)[2,3,5,4]
52keep[2,3,5,4]
63swap (5,4)[2,3,4,5]

Sorted result:

[2,3,4,5] [2,3,4,5]

Correctness

At each step, the algorithm ensures that the prefix up to index ii is locally ordered. When a violation occurs, swapping and stepping backward fixes the inversion. This process continues until all adjacent pairs are in order, which implies global sorted order.

Complexity

casecomparisonstime
bestnnO(n)O(n)
worstO(n2)O(n^2)O(n2)O(n^2)
averageO(n2)O(n^2)O(n2)O(n^2)

Space complexity:

O(1) O(1)

Properties

  • stable
  • in-place
  • conceptually simple

When to Use

Gnome sort is mainly useful for educational purposes. It demonstrates how local swaps can simulate insertion behavior. In practice, insertion sort is more efficient due to fewer swaps.

Implementation

def gnome_sort(a):
    i = 0
    n = len(a)

    while i < n:
        if i == 0 or a[i - 1] <= a[i]:
            i += 1
        else:
            a[i], a[i - 1] = a[i - 1], a[i]
            i -= 1

    return a
func GnomeSort[T constraints.Ordered](a []T) {
    i := 0
    n := len(a)

    for i < n {
        if i == 0 || a[i-1] <= a[i] {
            i++
        } else {
            a[i], a[i-1] = a[i-1], a[i]
            i--
        }
    }
}