Skip to content

Commit 90a8e6e

Browse files
authored
Update sorts/bubble_sort.py (TheAlgorithms#5802)
* Add missing type annotations in bubble_sort.py * Refactor bubble_sort function
1 parent 0b0214c commit 90a8e6e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Diff for: sorts/bubble_sort.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def bubble_sort(collection):
1+
from typing import Any
2+
3+
4+
def bubble_sort(collection: list[Any]) -> list[Any]:
25
"""Pure implementation of bubble sort algorithm in Python
36
47
:param collection: some mutable ordered collection with heterogeneous
@@ -28,9 +31,9 @@ def bubble_sort(collection):
2831
True
2932
"""
3033
length = len(collection)
31-
for i in range(length - 1):
34+
for i in reversed(range(length)):
3235
swapped = False
33-
for j in range(length - 1 - i):
36+
for j in range(i):
3437
if collection[j] > collection[j + 1]:
3538
swapped = True
3639
collection[j], collection[j + 1] = collection[j + 1], collection[j]

0 commit comments

Comments
 (0)