Skip to content

Commit 8caa96e

Browse files
committed
Updating
If bubble sort does a full loop without any change to list, it returns.
1 parent a23c181 commit 8caa96e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pygorithm/sorting/bubble_sort.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# Author: OMKAR PATHAK
2+
# Contributors: Mohamed Kiouaz
23
# Created On: 31st July 2017
34

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)
56

67
# Bubble Sorting algorithm
78
def sort(List):
89
for i in range(len(List)):
10+
stop = True
911
for j in range(len(List) - 1, i, -1):
1012
if List[j] < List[j - 1]:
13+
stop = False
1114
List[j], List[j - 1] = List[j - 1], List[j]
15+
if(stop == True):
16+
return List
1217
return List
1318

1419
# time complexities
1520
def time_complexities():
16-
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)'''
1722

1823
# easily retrieve the source code of the sort function
1924
def get_code():

0 commit comments

Comments
 (0)