HOME

TheInfoList



OR:


Gnome sort (nicknamed stupid sort) is a variation of the insertion sort sorting algorithm that does not use nested loops. Gnome sort was originally proposed by
Iranian Iranian may refer to: * Iran, a sovereign state * Iranian peoples, the speakers of the Iranian languages. The term Iranic peoples is also used for this term to distinguish the pan ethnic term from Iranian, used for the people of Iran * Iranian lan ...
computer scientist Hamid Sarbazi-Azad (professor of Computer Science and Engineering at
Sharif University of Technology Sharif University of Technology (SUT; fa, دانشگاه صنعتی شریف) is a public research university in Tehran, Iran. It is widely considered as the nation's most prestigious and leading institution for science, technology, engineering, ...
) in 2000. The sort was first called ''stupid sort'' (not to be confused with bogosort), and then later described by
Dick Grune Dick Grune is a Dutch computer scientist and university lecturer best known for inventing and developing the first version of the Concurrent Versions System (CVS). Grune was involved in the construction of Algol 68 compilers in the 1970s and the A ...
and named ''gnome sort''. Gnome sort performs at least as many comparisons as insertion sort and has the same asymptotic runtime 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''(''n''2) but tends towards ''O''(''n'') if the list is initially almost sorted.''Almost sorted'' means that each item in the list is not far from its proper position (not farther than some small constant distance).
Dick Grune Dick Grune is a Dutch computer scientist and university lecturer best known for inventing and developing the first version of the Concurrent Versions System (CVS). Grune was involved in the construction of Algol 68 compilers in the 1970s and the A ...
described the sorting method with the following story:


Pseudocode

Here is
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
for the gnome sort using a zero-based array: 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


Example

Given an unsorted array, a =
, 3, 2, 4 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
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 variable pos.


Notes


References


External links


Gnome sort
{{DEFAULTSORT:Gnome Sort Sorting algorithms Comparison sorts Stable sorts