Skip to content

Commit 1a24f39

Browse files
authored
Enhance bubble sort docstrings and type hints
1 parent 63180d7 commit 1a24f39

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

sorts/bubble_sort.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Any
1+
from typing import Sequence, TypeVar
22

33

4-
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
4+
T = TypeVar("T", int, float, str) # comparable types
5+
def bubble_sort_iterative(collection: list[T]) -> list[T]:
56
"""Pure implementation of bubble sort algorithm in Python
67
78
:param collection: some mutable ordered collection with heterogeneous
@@ -54,7 +55,7 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5455
return collection
5556

5657

57-
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
58+
def bubble_sort_recursive(collection: list[T]) -> list[T]:
5859
"""It is similar iterative bubble sort but recursive.
5960
6061
:param collection: mutable ordered sequence of elements
@@ -63,7 +64,7 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
6364
Examples:
6465
>>> bubble_sort_recursive([0, 5, 2, 3, 2])
6566
[0, 2, 2, 3, 5]
66-
>>> bubble_sort_iterative([])
67+
>>> bubble_sort_recursive([])
6768
[]
6869
>>> bubble_sort_recursive([-2, -45, -5])
6970
[-45, -5, -2]

0 commit comments

Comments
 (0)