Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions sorts/gnome_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
python3 gnome_sort.py
"""

from typing import Protocol

def gnome_sort(lst: list) -> list:

class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...


def gnome_sort[T: Comparable](lst: list[T]) -> list[T]:
"""
Pure implementation of the gnome sort algorithm in Python

Expand All @@ -39,7 +45,7 @@ def gnome_sort(lst: list) -> list:
i = 1

while i < len(lst):
if lst[i - 1] <= lst[i]:
if not lst[i] < lst[i - 1]:
i += 1
else:
lst[i - 1], lst[i] = lst[i], lst[i - 1]
Expand Down
1 change: 1 addition & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def test_sort_matches_builtin(sort, case):
bubble_sort_iterative,
bubble_sort_recursive,
circle_sort,
gnome_sort,
insertion_sort,
merge_sort,
selection_sort,
Expand Down
Loading