Published by Dimitris Kougioumtzis · Updated
Walk forward when ordered and backward after a swap.
Advance over an ordered pair, but step backward after an exchange to repair the newly exposed pair.
When to use Gnome sort
Learning local invariant repair through backtracking.
Choosing an alternative
Insertion sort shifts a value through its prefix more directly.
Worked example: input and state changes
Example input
38, 12, 65, 8, 43, 27, 91, 19
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 38, 12, 65, 8, 43, 27, 91, 19 ], "active": [] }Step 12: Inspect neighboring values.
State at this checkpoint
{ "values": [ 8, 12, 38, 65, 43, 27, 91, 19 ], "active": [ -1, 0 ] }Step 24: Inspect neighboring values.
State at this checkpoint
{ "values": [ 8, 12, 38, 27, 43, 65, 91, 19 ], "active": [ 2, 3 ] }Step 35: Inspect neighboring values.
State at this checkpoint
{ "values": [ 8, 12, 27, 38, 43, 19, 65, 91 ], "active": [ 4, 5 ] }Step 47: Complete: values are in ascending order.
State at this checkpoint
{ "values": [ 8, 12, 19, 27, 38, 43, 65, 91 ], "active": [] }
Expected final state
Complete: values are in ascending order.
Complexity and input bounds
Family: Sorting. Time complexity: O(n²).
Space complexity: O(1) auxiliary space.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
2ā24 comma-separated integers between 0 and 99.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Guard the left boundary and allow equal values to advance.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 | // Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n) { let i = 1; while (i < n) { if (i == 0 || a[i] >= a[i - 1]) { i++; } else { let t = a[i]; a[i] = a[i - 1]; a[i - 1] = t; i--; } } return 0; } |
Source
NIST cites Hamid Sarbazi-Azad's 2000 newsletter and Dick Grune's later description. An accessible original newsletter copy has not been verified; the link is an authoritative algorithm reference.
- Gnome sort, Dictionary of Algorithms and Data Structures
Paul E. Black (NIST) (2022). Algorithm reference.