Skip to content

Commit

Permalink
Optimize bubbe sort (#417)
Browse files Browse the repository at this point in the history
* Optimize bubbe sort

- as after each step of 'outer-loop', one number reaches to its right position. Thus inner loop does not need to run for 'n' times in every cycle of 'outer-loop'.

* Followed PEP for PR #417
  • Loading branch information
VARoDeK authored and goswami-rahul committed Oct 3, 2018
1 parent 30ca4ae commit 89aabde
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions algorithms/sort/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ def swap(i, j):
iteration = 0
if simulation:
print("iteration",iteration,":",*arr)

x = -1
while swapped:
swapped = False
for i in range(1, n):
x = x + 1
for i in range(1, n-x):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
Expand Down

0 comments on commit 89aabde

Please sign in to comment.