Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions sort/sorting/gnome_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def gnome_sort(arr):
"""
Gnome Sort Algorithm

Gnome Sort works similarly to Insertion Sort.
It swaps elements backward until they are in correct order.

Time Complexity:
Worst & Average: O(n^2)
Best Case: O(n)

Space Complexity: O(1)
"""

index = 0
n = len(arr)

while index < n:
if index == 0:
index += 1
elif arr[index] >= arr[index - 1]:
index += 1
else:
# Swap elements
arr[index], arr[index - 1] = arr[index - 1], arr[index]
index -= 1

return arr


# Example usage
if __name__ == "__main__":
numbers = [34, 2, 10, 6, 7]
print("Sorted array:", gnome_sort(numbers))