-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble-Sort.py
31 lines (24 loc) · 1008 Bytes
/
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
######Simple Approach########
def BubbleSort(arr):
n = len(arr)
for i in range(n-1): #no need to check for last element as it will be already in correct place just after first iteration
for j in range(n-1-i): #Last i element will be in correct position so, do not compare them
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j] #swapping
#####Optimized Approach######
def BubbleSort1(arr):
n = len(arr)
for i in range(n-1):
swapped = False
for j in range(n-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
#If no swapping takes place in inner loop, means array is already sorted, so exit the loops
if swapped == False:
break
#Taking the input
array = list(map(int,input("Enter your array (as space separated integers) : ").split()))
#Passing array to the function
BubbleSort1(array)
print(array)