Update sorts/bubble_sort.py (#5802)

* Add missing type annotations in bubble_sort.py

* Refactor bubble_sort function
This commit is contained in:
Dylan Buchi 2023-07-31 15:50:00 -03:00 committed by GitHub
parent 0b0214c42f
commit 90a8e6e0d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,7 @@
def bubble_sort(collection): from typing import Any
def bubble_sort(collection: list[Any]) -> list[Any]:
"""Pure implementation of bubble sort algorithm in Python """Pure implementation of bubble sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous :param collection: some mutable ordered collection with heterogeneous
@ -28,9 +31,9 @@ def bubble_sort(collection):
True True
""" """
length = len(collection) length = len(collection)
for i in range(length - 1): for i in reversed(range(length)):
swapped = False swapped = False
for j in range(length - 1 - i): for j in range(i):
if collection[j] > collection[j + 1]: if collection[j] > collection[j + 1]:
swapped = True swapped = True
collection[j], collection[j + 1] = collection[j + 1], collection[j] collection[j], collection[j + 1] = collection[j + 1], collection[j]