Skip to content

Commit 9e3f52d

Browse files
authored
Create Bubble_Sorting.py
1 parent 670a63a commit 9e3f52d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Sorting_Algorithms/Bubble_Sorting.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Bubble Sorting Algorithm - Inplace and Stable sorting algorithm
2+
3+
arr = [6,1,5,-9,2,12,4,0]
4+
#arr = [1,2,3,4,5,6]
5+
n = len(arr)
6+
isSwapped = 0
7+
for i in range(1,n):
8+
# This loop will move the max element of the left unsorted part to it's
9+
# correct position.
10+
for j in range(n-1):
11+
if arr[j] > arr[j+1]:
12+
# Swapping
13+
arr[j+1], arr[j] = arr[j], arr[j+1]
14+
isSwapped = 1
15+
print(f'After Iteration - {i}: {arr}')
16+
# If not swapped, then the array is already sorted
17+
if not isSwapped:
18+
break
19+
20+
21+
print(f'\nAfter Sorting: {arr}')

0 commit comments

Comments
 (0)