Skip to content

Commit

Permalink
Break if the collection is sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
Hossam Al-Dokkani committed Jun 23, 2018
1 parent cb6c82c commit 9489e85
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sorts/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 9489e85

Please sign in to comment.