Gnome sort

Gnome sort

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 flowerpotsFact|date=October 2007 and is described on Dick Grune's [http://www.cs.vu.nl/~dick/gnomesort.html Gnome sort page]

It is conceptually simple, requiring no nested loops. The running time is O("n"²), but 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.

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 ascending 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 := 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.

External links

* [http://www.cs.vu.nl/~dick/gnomesort.html Gnome sort]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Gnome sort — El algoritmo de ordenación conocido como gNome sort tiene una historia de invención cuasi paralela. Durante un tiempo existió la polémica sobre su invención, finalmente atribuída a Hamid Sarbazi Azad quien lo desarrolló en en año 2000 y al que… …   Wikipedia Español

  • Sort (Unix) — Pour les articles homonymes, voir sort. sort est une commande POSIX qui permet de trier les lignes de fichiers texte. Par défaut, sort affiche l ensemble des lignes des fichiers qu on lui passe en paramètre triées par ordre croissant de la table… …   Wikipédia en Français

  • Sort (Lerida) — Sort (Lérida) Demande de traduction Sort → …   Wikipédia en Français

  • Gnome — This article is about the humanoid creature. For the computing desktop environment, see GNOME. For the lawn ornament, see garden gnome. For other uses, see Gnome (disambiguation). A gnome /ˈnoʊm …   Wikipedia

  • Gnome-panel — infobox software name = gnome panel caption = gnome panel active developer = GNOME latest release version = 2.20 latest release date = 19 September 2007 operating system = Cross platform genre = Desktop environment platform = GNOME license = GNU… …   Wikipedia

  • sort (Unix) — Pour les articles homonymes, voir sort. sort est une commande POSIX qui permet de trier des fichiers ou leurs contenus. Par défaut, sort affiche l ensemble des lignes des fichiers qu on lui passe en paramètre triées par ordre croissant de la… …   Wikipédia en Français

  • Comb sort — Class Sorting algorithm Data structure Array Worst case performance O(n log n)[1] …   Wikipedia

  • Merge sort — Example of merge sort sorting a list of random dots. Class Sorting algorithm Data structure Array Worst case performance O(n log n) …   Wikipedia

  • Selection sort — Infobox Algorithm class=Sorting algorithm data=Array time= О(n²) space= О(n) total, O(1) auxiliary optimal=Not usuallySelection sort is a sorting algorithm, specifically an in place comparison sort. It has O( n 2) complexity, making it… …   Wikipedia

  • Counting sort — In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”