Break if the collection is sorted

This commit is contained in:
Hossam Al-Dokkani 2018-06-23 17:01:06 +02:00
parent cb6c82cb0f
commit 9489e8512d

View File

@ -32,10 +32,12 @@ def bubble_sort(collection):
"""
length = len(collection)
for i in range(length):
swapped = False
for j in range(length-1):
if collection[j] > collection[j+1]:
swapped = True
collection[j], collection[j+1] = collection[j+1], collection[j]
if not swapped: break # Stop iteration if the collection is sorted.
return collection