From 428aeb86eed8fa291b3afe2f2c44181af13c0f01 Mon Sep 17 00:00:00 2001 From: VARNIT KUMAR <161148074+vannu07@users.noreply.github.com> Date: Sat, 11 Oct 2025 14:20:49 +0530 Subject: [PATCH 1/3] feat: add recursive bubble sort algorithm with doctests and type hints Implement recursive Bubble Sort algorithm with doctests. --- sorts/bubble_sort_recursive.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 sorts/bubble_sort_recursive.py diff --git a/sorts/bubble_sort_recursive.py b/sorts/bubble_sort_recursive.py new file mode 100644 index 000000000000..3be74028f56e --- /dev/null +++ b/sorts/bubble_sort_recursive.py @@ -0,0 +1,39 @@ +from typing import List + + +def bubble_sort_recursive(arr: List[int]) -> List[int]: + """ + Sorts a list of integers using the recursive Bubble Sort algorithm. + + >>> bubble_sort_recursive([5, 1, 4, 2, 8]) + [1, 2, 4, 5, 8] + >>> bubble_sort_recursive([]) + [] + >>> bubble_sort_recursive([1]) + [1] + >>> bubble_sort_recursive([3, 3, 2, 1]) + [1, 2, 3, 3] + >>> bubble_sort_recursive([-1, 5, 0, -2]) + [-2, -1, 0, 5] + """ + n = len(arr) + if n <= 1: + return arr + + swapped = False + for i in range(n - 1): + if arr[i] > arr[i + 1]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + swapped = True + + # Base case: if no elements were swapped, the array is sorted + if not swapped: + return arr + + # Recursive call for the remaining unsorted part + return bubble_sort_recursive(arr[:-1]) + [arr[-1]] + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 5ef8b6a6c88d89e17caa53016c1a8d8cb8bcd541 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 08:56:44 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/bubble_sort_recursive.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/bubble_sort_recursive.py b/sorts/bubble_sort_recursive.py index 3be74028f56e..e0afc6e3c375 100644 --- a/sorts/bubble_sort_recursive.py +++ b/sorts/bubble_sort_recursive.py @@ -36,4 +36,5 @@ def bubble_sort_recursive(arr: List[int]) -> List[int]: if __name__ == "__main__": import doctest + doctest.testmod() From 937173c301c603e853a346ddc0007b847b335034 Mon Sep 17 00:00:00 2001 From: VARNIT KUMAR <161148074+vannu07@users.noreply.github.com> Date: Sat, 11 Oct 2025 14:35:56 +0530 Subject: [PATCH 3/3] Refactor bubble_sort_recursive function Refactor bubble_sort_recursive to use built-in list type and improve return statement. --- sorts/bubble_sort_recursive.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/sorts/bubble_sort_recursive.py b/sorts/bubble_sort_recursive.py index e0afc6e3c375..f50707c14620 100644 --- a/sorts/bubble_sort_recursive.py +++ b/sorts/bubble_sort_recursive.py @@ -1,7 +1,4 @@ -from typing import List - - -def bubble_sort_recursive(arr: List[int]) -> List[int]: +def bubble_sort_recursive(arr: list[int]) -> list[int]: """ Sorts a list of integers using the recursive Bubble Sort algorithm. @@ -26,12 +23,10 @@ def bubble_sort_recursive(arr: List[int]) -> List[int]: arr[i], arr[i + 1] = arr[i + 1], arr[i] swapped = True - # Base case: if no elements were swapped, the array is sorted if not swapped: return arr - # Recursive call for the remaining unsorted part - return bubble_sort_recursive(arr[:-1]) + [arr[-1]] + return [*bubble_sort_recursive(arr[:-1]), arr[-1]] if __name__ == "__main__":