Skip to content

Commit a9ba564

Browse files
authored
Update bubble_sort.py
1 parent f6459d4 commit a9ba564

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

algorithms/sorting/bubble_sort.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
Best time scenario is when array is already sorted - O(n)
44
"""
55

6-
76
def bubble_sort(array):
7+
n = len(array)
8+
for i in range(n):
9+
for j in range(0, n-i-1):
10+
if array[j] > array[j+1]:
11+
array[j], array[j+1] = array[j+1], array[j]
12+
return array
13+
14+
15+
def bubble_sort_optimized(array):
16+
"""
17+
Optimizes on bubble sort by taking care of already swapped cases
18+
Reference - https://github.com/prabhupant/python-ds/pull/346
19+
"""
820
has_swapped = True
921

1022
num_of_iterations = 0

0 commit comments

Comments
 (0)