From 9c8cd8afa57d7e5bcf7a44ce76ab6735dae1e35d Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Tue, 9 Nov 2021 21:10:03 -0300 Subject: [PATCH 1/2] Add missing type annotations in bubble_sort.py --- sorts/bubble_sort.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index d4f0d25ca77c..ee1db4ba102e 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -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 :param collection: some mutable ordered collection with heterogeneous From 8c07864507c9dc7f68755b6645cd5ce33e57da0a Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Tue, 9 Nov 2021 21:16:46 -0300 Subject: [PATCH 2/2] Refactor bubble_sort function --- sorts/bubble_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index ee1db4ba102e..a497245da658 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -31,9 +31,9 @@ def bubble_sort(collection: list[Any]) -> list[Any]: True """ length = len(collection) - for i in range(length - 1): + for i in reversed(range(length)): swapped = False - for j in range(length - 1 - i): + for j in range(i): if collection[j] > collection[j + 1]: swapped = True collection[j], collection[j + 1] = collection[j + 1], collection[j]