Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Gnome sort
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Pseudocode == 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 takes the following steps during the while loop. The current position is highlighted in bold and indicated as a value of the variable <code>pos</code>. {| class="wikitable" |- ! Current array ! <code>pos</code> ! 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 |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)