mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 08:47:01 +00:00
Merge pull request #321 from aldokkani/master
Break if the collection is sorted
This commit is contained in:
commit
d4b4b7ba35
|
@ -32,10 +32,12 @@ def bubble_sort(collection):
|
||||||
"""
|
"""
|
||||||
length = len(collection)
|
length = len(collection)
|
||||||
for i in range(length):
|
for i in range(length):
|
||||||
|
swapped = False
|
||||||
for j in range(length-1):
|
for j in range(length-1):
|
||||||
if collection[j] > collection[j+1]:
|
if collection[j] > collection[j+1]:
|
||||||
|
swapped = True
|
||||||
collection[j], collection[j+1] = collection[j+1], collection[j]
|
collection[j], collection[j+1] = collection[j+1], collection[j]
|
||||||
|
if not swapped: break # Stop iteration if the collection is sorted.
|
||||||
return collection
|
return collection
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user