1 parent a23c181 commit 8caa96eCopy full SHA for 8caa96e
1 file changed
pygorithm/sorting/bubble_sort.py
@@ -1,19 +1,24 @@
1
# Author: OMKAR PATHAK
2
+# Contributors: Mohamed Kiouaz
3
# Created On: 31st July 2017
4
-# Best O(n^2); Average O(n^2); Worst O(n^2)
5
+# Best O(n); Average O(n*(n-1)/4); Worst O(n^2)
6
7
# Bubble Sorting algorithm
8
def sort(List):
9
for i in range(len(List)):
10
+ stop = True
11
for j in range(len(List) - 1, i, -1):
12
if List[j] < List[j - 1]:
13
+ stop = False
14
List[j], List[j - 1] = List[j - 1], List[j]
15
+ if(stop == True):
16
+ return List
17
return List
18
19
# time complexities
20
def time_complexities():
- return '''Best Case: O(n ^ 2), Average Case: O(n ^ 2), Worst Case: O(n ^ 2)'''
21
+ return '''Best case O(n); Average case O(n * (n - 1) / 4); Worst case O(n ^ 2)'''
22
23
# easily retrieve the source code of the sort function
24
def get_code():
0 commit comments