Jump to content

Gnome sort: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Started requested copyedit (from tag). WP:GOCE.
Rescuing 4 sources and tagging 0 as dead. #IABot (v2.0beta10)
Line 12: Line 12:
|optimal= No
|optimal= No
}}
}}
'''Gnome sort''' (dubbed '''Stupid sort''') is a [[sorting algorithm]] originally proposed by an [[Iran]]ian computer scientist [[Hamid Sarbazi-Azad]] (Professor of Computer Engineering at [[Sharif University of Technology]])<ref>{{Cite web|url=http://sharif.edu/~azad/|title=Hamid Sarbazi-Azad profile page|last=Hamid|first=Sarbazi-Azad|date=|website=|archive-url=|archive-date=|dead-url=|access-date=October 16, 2018}}</ref> in 2000. The sort was first called "stupid sort"<ref>{{cite journal
'''Gnome sort''' (dubbed '''Stupid sort''') is a [[sorting algorithm]] originally proposed by an [[Iran]]ian computer scientist [[Hamid Sarbazi-Azad]] (Professor of Computer Engineering at [[Sharif University of Technology]])<ref>{{Cite web|url=http://sharif.edu/~azad/|title=Hamid Sarbazi-Azad profile page|last=Hamid|first=Sarbazi-Azad|date=|website=|archive-url=https://web.archive.org/web/20181016164904/http://sharif.edu/~azad/#|archive-date=2018-10-16|dead-url=no|access-date=October 16, 2018|df=}}</ref> in 2000. The sort was first called "stupid sort"<ref>{{cite journal
| last = Sarbazi-Azad
|last = Sarbazi-Azad
| first = Hamid
|first = Hamid
| last2 =
|last2 =
| first2 =
|first2 =
| date = 2 October 2000
|date = 2 October 2000
| title = Stupid Sort: A new sorting algorithm
|title = Stupid Sort: A new sorting algorithm
| url = http://sina.sharif.edu/~azad/stupid-sort.PDF
|url = http://sina.sharif.edu/~azad/stupid-sort.PDF
| journal = Newsletter
|journal = Newsletter
| publisher = Computing Science Department, Univ. of Glasgow
|publisher = Computing Science Department, Univ. of Glasgow
| volume =
|volume =
| issue = 599
|issue = 599
| page = 4
|page = 4
| bibcode =
|bibcode =
| doi =
|doi =
| accessdate = 25 November 2014
|accessdate = 25 November 2014
|archive-url = https://web.archive.org/web/20120307235904/http://sina.sharif.edu/~azad/stupid-sort.PDF#
}}</ref> (not to be confused with [[bogosort]]), and then later on described by [[Dick Grune]] and named "gnome sort".<ref name="DGrune">{{cite web|url=http://www.dickgrune.com/Programs/gnomesort.html |title=Gnome Sort - The Simplest Sort Algorithm |website=Dickgrune.com |date=2000-10-02 |accessdate=2017-07-20}}</ref>
|archive-date = 2012-03-07
|dead-url = no
|df =
}}</ref> (not to be confused with [[bogosort]]), and then later on described by [[Dick Grune]] and named "gnome sort".<ref name="DGrune">{{cite web |url=http://www.dickgrune.com/Programs/gnomesort.html |title=Gnome Sort - The Simplest Sort Algorithm |website=Dickgrune.com |date=2000-10-02 |accessdate=2017-07-20 |archive-url=https://web.archive.org/web/20170831222005/https://dickgrune.com/Programs/gnomesort.html# |archive-date=2017-08-31 |dead-url=no |df= }}</ref>


The 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, similar to a [[bubble sort]]. It is conceptually simple, requiring no [[Nested loop join|nested loops]]. The average running time is [[Big O notation|''O'']](''n''<sup>2</sup>) but tends towards ''O''(''n'') if the list is initially almost sorted.<ref>{{cite web |
The 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, similar to a [[bubble sort]]. It is conceptually simple, requiring no [[Nested loop join|nested loops]]. The average running time is [[Big O notation|''O'']](''n''<sup>2</sup>) but tends towards ''O''(''n'') if the list is initially almost sorted.<ref>{{cite web
url=http://xlinux.nist.gov/dads/HTML/gnomeSort.html |
|url = http://xlinux.nist.gov/dads/HTML/gnomeSort.html
title=gnome sort|
|title = gnome sort
work=Dictionary of Algorithms and Data Structures |
|work = Dictionary of Algorithms and Data Structures
publisher=U.S. National Institute of Standards and Technology|
|publisher = U.S. National Institute of Standards and Technology
author=Paul E. Black|
|author = Paul E. Black
accessdate=2011-08-20
|accessdate = 2011-08-20
|archive-url = https://web.archive.org/web/20110811012120/http://xlinux.nist.gov/dads//HTML/gnomeSort.html#
|archive-date = 2011-08-11
|dead-url = no
|df =
}}</ref><ref group="note">‘Almost sorted’ in this case means that each item in the list is not farther than some small constant distance from its proper position. {{Copy edit-inline|date=September 2018}}</ref>
}}</ref><ref group="note">‘Almost sorted’ in this case means that each item in the list is not farther than some small constant distance from its proper position. {{Copy edit-inline|date=September 2018}}</ref>



Revision as of 21:57, 23 November 2018

Gnome sort
Visualisation of Gnome sort.
ClassSorting algorithm
Data structureArray
Worst-case performance
Best-case performance
Average performance
Worst-case space complexity auxiliary
OptimalNo

Gnome sort (dubbed Stupid sort) is a sorting algorithm originally proposed by an Iranian computer scientist Hamid Sarbazi-Azad (Professor of Computer Engineering at Sharif University of Technology)[1] in 2000. The sort was first called "stupid sort"[2] (not to be confused with bogosort), and then later on described by Dick Grune and named "gnome sort".[3]

The 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, similar to a bubble sort. It is conceptually simple, requiring no nested loops. The average running time is O(n2) but tends towards O(n) if the list is initially almost sorted.[4][note 1]

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 next to the previously swapped elements. It does not assume that elements forward of the current position are sorted, so it only needs to check the position directly previous to the swapped elements.

Description

Dick Grune described the sorting method with the following story:[3]

Gnome Sort is based on the technique used by the standard Dutch Garden Gnome (Du.: tuinkabouter).
Here is how a garden gnome sorts a line of flower pots.
Basically, he looks at the flower pot next to him and the previous one; if they are in the right order he steps one pot forward, otherwise, he swaps them and steps one pot backward.
Boundary conditions: if there is no previous pot, he steps forwards; if there is no pot next to him, he is done.

— "Gnome Sort - The Simplest Sort Algorithm". Dickgrune.com

Code

C#

An implementation in C#:

	public static void gnomeSort(int[] anArray)
	{
		int first = 1;

		while (first < anArray.Length)
		{
			if (anArray[first - 1] <= anArray[first]) 
			{
				first ++;
			} 
			else
			{
				int tmp = anArray[first - 1];
				anArray[first - 1] = anArray[first];
				anArray[first] = tmp;
				if (-- first == 0)
				{
					first = 1;
				}
			}

		}
	}

Here is pseudocode 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 = [5, 3, 2, 4], the gnome sort would take the following steps during the while loop. The "current position" is highlighted in bold:

Current array pos Condition in effect Action to take
[5, 3, 2, 4] 0 pos == 0 increment pos
[5, 3, 2, 4] 1 a[pos] < a[pos-1] swap, decrement pos
[3, 5, 2, 4] 0 pos == 0 increment pos
[3, 5, 2, 4] 1 a[pos] ≥ a[pos-1] increment pos
[3, 5, 2, 4] 2 a[pos] < a[pos-1] swap, decrement pos
[3, 2, 5, 4] 1 a[pos] < a[pos-1] swap, decrement pos
[2, 3, 5, 4] 0 pos == 0 increment pos
[2, 3, 5, 4] 1 a[pos] ≥ a[pos-1] increment pos
[2, 3, 5, 4] 2 a[pos] ≥ a[pos-1] increment pos:
[2, 3, 5, 4] 3 a[pos] < a[pos-1] swap, decrement pos
[2, 3, 4, 5] 2 a[pos] ≥ a[pos-1] increment pos
[2, 3, 4, 5] 3 a[pos] ≥ a[pos-1] increment pos
[2, 3, 4, 5] 4 pos == length(a) finished

Optimization

The gnome sort may be optimized by introducing a variable to store the position before traversing back toward the beginning of the list. 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[]):
    for pos in 1 to length(a):
        gnomeSort(a, pos)

procedure gnomeSort(a[], upperBound):
    pos := upperBound
    while pos > 0 and a[pos-1] > a[pos]:
        swap a[pos-1] and a[pos]
        pos := pos - 1

Notes

  1. ^ ‘Almost sorted’ in this case means that each item in the list is not farther than some small constant distance from its proper position. [needs copy edit]

References

  1. ^ Hamid, Sarbazi-Azad. "Hamid Sarbazi-Azad profile page". Archived from the original on 2018-10-16. Retrieved October 16, 2018. {{cite web}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  2. ^ Sarbazi-Azad, Hamid (2 October 2000). "Stupid Sort: A new sorting algorithm" (PDF). Newsletter (599). Computing Science Department, Univ. of Glasgow: 4. Archived from the original (PDF) on 2012-03-07. Retrieved 25 November 2014. {{cite journal}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  3. ^ a b "Gnome Sort - The Simplest Sort Algorithm". Dickgrune.com. 2000-10-02. Archived from the original on 2017-08-31. Retrieved 2017-07-20. {{cite web}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  4. ^ Paul E. Black. "gnome sort". Dictionary of Algorithms and Data Structures. U.S. National Institute of Standards and Technology. Archived from the original on 2011-08-11. Retrieved 2011-08-20. {{cite web}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)