We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f6459d4 commit a9ba564Copy full SHA for a9ba564
1 file changed
algorithms/sorting/bubble_sort.py
@@ -3,8 +3,20 @@
3
Best time scenario is when array is already sorted - O(n)
4
"""
5
6
-
7
def bubble_sort(array):
+ 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
20
has_swapped = True
21
22
num_of_iterations = 0
0 commit comments