Gnome Sort Visualizer
Speed:
SlowFast
Bars:
520
(10)148
[0]
90
[1]
117
[2]
183
[3]
156
[4]
148
[5]
75
[6]
139
[7]
99
[8]
186
[9]
Comparing
Swapping
Sorted
Default
Operation Logs
Click "Start Sort" to begin
Algorithm Info
Time Complexity: O(n²) worst and average case, O(n) best case
Space Complexity: O(1) - in-place sorting
Stable: Yes
How it works: Works like a garden gnome sorting flower pots: if current element is in right order, move forward; if not, swap with previous and move back.
Implementation
function gnomeSort(arr) {
let i = 0;
while (i < arr.length) {
if (i === 0 || arr[i - 1] <= arr[i]) {
i++;
} else {
[arr[i], arr[i - 1]] = [arr[i - 1], arr[i]];
i--;
}
}
return arr;
}