Skip to content

Commit 937173c

Browse files
authored
Refactor bubble_sort_recursive function
Refactor bubble_sort_recursive to use built-in list type and improve return statement.
1 parent 5ef8b6a commit 937173c

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

sorts/bubble_sort_recursive.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from typing import List
2-
3-
4-
def bubble_sort_recursive(arr: List[int]) -> List[int]:
1+
def bubble_sort_recursive(arr: list[int]) -> list[int]:
52
"""
63
Sorts a list of integers using the recursive Bubble Sort algorithm.
74
@@ -26,12 +23,10 @@ def bubble_sort_recursive(arr: List[int]) -> List[int]:
2623
arr[i], arr[i + 1] = arr[i + 1], arr[i]
2724
swapped = True
2825

29-
# Base case: if no elements were swapped, the array is sorted
3026
if not swapped:
3127
return arr
3228

33-
# Recursive call for the remaining unsorted part
34-
return bubble_sort_recursive(arr[:-1]) + [arr[-1]]
29+
return [*bubble_sort_recursive(arr[:-1]), arr[-1]]
3530

3631

3732
if __name__ == "__main__":

0 commit comments

Comments
 (0)