From 1a24f39a4b804fccac52566c77213c32cfd554d4 Mon Sep 17 00:00:00 2001 From: ibirothe <132349052+ibirothe@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:10:04 +0200 Subject: [PATCH 1/4] Enhance bubble sort docstrings and type hints --- sorts/bubble_sort.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 9ec3d5384f38..073a9e0f015b 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,7 +1,8 @@ -from typing import Any +from typing import Sequence, TypeVar -def bubble_sort_iterative(collection: list[Any]) -> list[Any]: +T = TypeVar("T", int, float, str) # comparable types +def bubble_sort_iterative(collection: list[T]) -> list[T]: """Pure implementation of bubble sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous @@ -54,7 +55,7 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]: return collection -def bubble_sort_recursive(collection: list[Any]) -> list[Any]: +def bubble_sort_recursive(collection: list[T]) -> list[T]: """It is similar iterative bubble sort but recursive. :param collection: mutable ordered sequence of elements @@ -63,7 +64,7 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]: Examples: >>> bubble_sort_recursive([0, 5, 2, 3, 2]) [0, 2, 2, 3, 5] - >>> bubble_sort_iterative([]) + >>> bubble_sort_recursive([]) [] >>> bubble_sort_recursive([-2, -45, -5]) [-45, -5, -2] From dbe827d00fb424506c1be987d13f2096bcd994e9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 13 Sep 2025 13:13:11 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/bubble_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 073a9e0f015b..5d3f5af6f975 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -2,6 +2,8 @@ T = TypeVar("T", int, float, str) # comparable types + + def bubble_sort_iterative(collection: list[T]) -> list[T]: """Pure implementation of bubble sort algorithm in Python From e1383d951b12eca47a4ad0a212319650846dadd1 Mon Sep 17 00:00:00 2001 From: ibirothe <132349052+ibirothe@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:21:15 +0200 Subject: [PATCH 3/4] Updatetyping to comply to code style guidelines --- sorts/bubble_sort.py | 63 ++++++++++---------------------------------- 1 file changed, 14 insertions(+), 49 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 5d3f5af6f975..812f882d51fe 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,15 +1,14 @@ -from typing import Sequence, TypeVar - +from typing import TypeVar T = TypeVar("T", int, float, str) # comparable types def bubble_sort_iterative(collection: list[T]) -> list[T]: - """Pure implementation of bubble sort algorithm in Python + """ + Pure implementation of bubble sort algorithm in Python. - :param collection: some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending + :param collection: list of comparable elements + :return: the same collection ordered in ascending order Examples: >>> bubble_sort_iterative([0, 5, 2, 3, 2]) @@ -20,47 +19,30 @@ def bubble_sort_iterative(collection: list[T]) -> list[T]: [-45, -5, -2] >>> bubble_sort_iterative([-23, 0, 6, -4, 34]) [-23, -4, 0, 6, 34] - >>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) - True - >>> bubble_sort_iterative([]) == sorted([]) - True - >>> bubble_sort_iterative([-2, -45, -5]) == sorted([-2, -45, -5]) - True - >>> bubble_sort_iterative([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) - True >>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e']) True >>> bubble_sort_iterative(['z', 'a', 'y', 'b', 'x', 'c']) ['a', 'b', 'c', 'x', 'y', 'z'] >>> bubble_sort_iterative([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6]) [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7] - >>> bubble_sort_iterative([1, 3.3, 5, 7.7, 2, 4.4, 6]) - [1, 2, 3.3, 4.4, 5, 6, 7.7] - >>> import random - >>> collection_arg = random.sample(range(-50, 50), 100) - >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg) - True - >>> import string - >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100) - >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg) - True """ length = len(collection) for i in reversed(range(length)): swapped = False for j in range(i): if collection[j] > collection[j + 1]: - swapped = True collection[j], collection[j + 1] = collection[j + 1], collection[j] + swapped = True if not swapped: - break # Stop iteration if the collection is sorted. + break return collection def bubble_sort_recursive(collection: list[T]) -> list[T]: - """It is similar iterative bubble sort but recursive. + """ + Recursive implementation of bubble sort. - :param collection: mutable ordered sequence of elements + :param collection: list of comparable elements :return: the same list in ascending order Examples: @@ -72,32 +54,12 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]: [-45, -5, -2] >>> bubble_sort_recursive([-23, 0, 6, -4, 34]) [-23, -4, 0, 6, 34] - >>> bubble_sort_recursive([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) - True - >>> bubble_sort_recursive([]) == sorted([]) - True - >>> bubble_sort_recursive([-2, -45, -5]) == sorted([-2, -45, -5]) - True - >>> bubble_sort_recursive([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) - True >>> bubble_sort_recursive(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e']) True >>> bubble_sort_recursive(['z', 'a', 'y', 'b', 'x', 'c']) ['a', 'b', 'c', 'x', 'y', 'z'] >>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6]) [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7] - >>> bubble_sort_recursive([1, 3.3, 5, 7.7, 2, 4.4, 6]) - [1, 2, 3.3, 4.4, 5, 6, 7.7] - >>> bubble_sort_recursive(['a', 'Z', 'B', 'C', 'A', 'c']) - ['A', 'B', 'C', 'Z', 'a', 'c'] - >>> import random - >>> collection_arg = random.sample(range(-50, 50), 100) - >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg) - True - >>> import string - >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100) - >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg) - True """ length = len(collection) swapped = False @@ -106,7 +68,10 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]: collection[i], collection[i + 1] = collection[i + 1], collection[i] swapped = True - return collection if not swapped else bubble_sort_recursive(collection) + if not swapped: + return collection + return bubble_sort_recursive(collection) + if __name__ == "__main__": From ee4c6c124d1acd11655081e3fc52479856f3a367 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 13 Sep 2025 13:21:37 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/bubble_sort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 812f882d51fe..71e710ade193 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -73,7 +73,6 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]: return bubble_sort_recursive(collection) - if __name__ == "__main__": import doctest from random import sample