mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Update sorts/bubble_sort.py
(#5802)
* Add missing type annotations in bubble_sort.py * Refactor bubble_sort function
This commit is contained in:
parent
0b0214c42f
commit
90a8e6e0d2
|
@ -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]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user