Gnome Sort
Gnome Sort :
Author(s) : Hamid Sarbazi-Azad Date : 2000Gnome sort, originally proposed as stupid sort, is a sorting algorithm which is similar to insertion sort in that it works with one item at a time but gets the item to the proper place by a series of swaps, similar to a bubble sort. It is conceptually simple, requiring no nested loops. The name comes from the behavior of a Dutch garden gnome sorting a line of flower pots.
| Time Complexity | O(n²) |
| Best Case | O(n) |
| Worst Case | O(n²) |
| Space Complexity | O(1) |
| Stable | Yes |
Code Integration:
* the code might contain some bugs or may not work correctly
def gnome_sort(arr):
n = len(arr)
index = 0
while index < n:
if index == 0:
index += 1
if arr[index] >= arr[index - 1]:
index += 1
else:
arr[index], arr[index - 1] = arr[index - 1], arr[index]
index -= 1
return arr
function gnomeSort(arr) {
let n = arr.length;
let index = 0;
while (index < n) {
if (index === 0) index++;
if (arr[index] >= arr[index - 1]) {
index++;
} else {
[arr[index], arr[index - 1]] = [arr[index - 1], arr[index]];
index--;
}
}
return arr;
}
public static void gnomeSort(int[] arr) {
int n = arr.length;
int index = 0;
while (index < n) {
if (index == 0) index++;
if (arr[index] >= arr[index - 1]) {
index++;
} else {
int temp = arr[index];
arr[index] = arr[index - 1];
arr[index - 1] = temp;
index--;
}
}
}
func gnomeSort(arr []int) {
n := len(arr)
index := 0
for index < n {
if index == 0 {
index++
}
if arr[index] >= arr[index-1] {
index++
} else {
arr[index], arr[index-1] = arr[index-1], arr[index]
index--
}
}
}
void gnomeSort(int arr[], int n) {
int index = 0;
while (index < n) {
if (index == 0) index++;
if (arr[index] >= arr[index - 1]) {
index++;
} else {
int temp = arr[index];
arr[index] = arr[index - 1];
arr[index - 1] = temp;
index--;
}
}
}