-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06_Bubble_Sort.py
34 lines (26 loc) · 947 Bytes
/
06_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
"""
Bubble Sorting Algorithm
Run the code and see that BubbleSortB is more time efficient than BubbleSortA
"""
def BubbleSortA(arr):
iterations = 0
for j in range(len(arr)-1):
for i in range(len(arr)-1):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
iterations+=1
return arr, iterations
def BubbleSortB(arr):
iterations=0
for j in range(len(arr)-1):
for i in range(len(arr)-1-j):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
iterations+=1
return arr, iterations
arr = [5, 1, 4, 2, 6, 8, 3, 7, 9] #9 element
# arr = int(input())
sortedarrA, interations = BubbleSortA(arr)
print("InterationsA = {}\nsortedarrA = {}".format(interations, sortedarrA))
sortedarrB, interations = BubbleSortB(arr)
print("InterationsB = {}\nsortedarrB = {}".format(interations, sortedarrB))