-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathBubble_Sort.py
87 lines (72 loc) · 2.51 KB
/
Bubble_Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#In Bubble Sort, the largest value is bubbled up in every pass.
#Every two adjacent items are compared and they are swapped if they are in the wrong order.
#This way, after every pass, the largest element reaches to the end of the array.
#Time complexity of Bubble Sort in Worst and Average Case is O(n^2) and in best case, its O(n)
def bubble_sort(array):
count = 0
for i in range(len(array)-1): #-1 because when only 1 item will be left, we don't need to sort that
print(array)
for j in range(len(array)-i-1): #In every iteration of the outer loop, one number gets sorted. So the inner loop will run only for the unsorted part
count += 1
if array[j] > array[j+1]: #If two adjacent elements in the wrong order are found, they are swapped
array[j], array[j+1] = array[j+1], array[j]
#print(f'Number of comparisons = {count}')
return (f'{array} \nNumber of comparisons = {count}')
array = [5,9,3,10,45,2,0]
print(bubble_sort(array))
'''
[5, 9, 3, 10, 45, 2, 0]
[5, 3, 9, 10, 2, 0, 45]
[3, 5, 9, 2, 0, 10, 45]
[3, 5, 2, 0, 9, 10, 45]
[3, 2, 0, 5, 9, 10, 45]
[2, 0, 3, 5, 9, 10, 45]
[0, 2, 3, 5, 9, 10, 45]
[0, 2, 3, 5, 9, 10, 45]
Number of comparisons = 21
'''
sorted_array = [5,6,7,8,9]
print(bubble_sort(sorted_array))
'''
[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
Number of comparisons = 10
'''
#We can optimize the bubble sort slightly by adding a new boolean variable
#which keeps track of wehether any swaps were done in the last iteration or not
#This way, if say halfway through the loops, the array becomes completely sorted, then we won't do unnecessary comparisons
def optimized_bubble_sort(array):
count = 0
for i in range(len(array) - 1):
swap = False
print(array)
for j in range(len(array) - i - 1):
count += 1
if array[j] > array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
swap = True
if swap==False:
return (f'{array} \nNumber of comparisons = {count}')
return (f'{array} \nNumber of comparisons = {count}')
array1 = [5,9,3,10,45,2,0]
print(optimized_bubble_sort(array1))
'''
[5, 9, 3, 10, 45, 2, 0]
[5, 3, 9, 10, 2, 0, 45]
[3, 5, 9, 2, 0, 10, 45]
[3, 5, 2, 0, 9, 10, 45]
[3, 2, 0, 5, 9, 10, 45]
[2, 0, 3, 5, 9, 10, 45]
[0, 2, 3, 5, 9, 10, 45]
Number of comparisons = 21
'''
sorted_array1 = [5,6,7,8,9]
print(optimized_bubble_sort(sorted_array1))
'''
[5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
Number of comparisons = 4
'''