Skip to content

Commit 89aabde

Browse files
VARoDeKgoswami-rahul
authored andcommitted
Optimize bubbe sort (keon#417)
* 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 keon#417
1 parent 30ca4ae commit 89aabde

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

algorithms/sort/bubble_sort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def swap(i, j):
2020
iteration = 0
2121
if simulation:
2222
print("iteration",iteration,":",*arr)
23-
23+
x = -1
2424
while swapped:
2525
swapped = False
26-
for i in range(1, n):
26+
x = x + 1
27+
for i in range(1, n-x):
2728
if arr[i - 1] > arr[i]:
2829
swap(i - 1, i)
2930
swapped = True

0 commit comments

Comments
 (0)