# Sorting algorithms

# Bubble sort

# Swapping in Python

a = 30
b = 90
print(a)
print(b)
1
2
3
4

OUTPUT

30 90

a = 30
b = 90
a,b = b,a
print(a)
print(b)
1
2
3
4
5

OUTPUT

90 30

# Bubble sort algorithm

# Initialize

img

1st Iteration

img

if 5 > 9 then Swap
1

img

if 9 > 3 then Swap
1

img

Swap
1

img

if 9 > 1 then Swap
1

img

Stop
1

img

2nd Iteration

img

if 5 > 3 then Swap
1

img

Swap
1

img

if 5 > 1 then Swap
1

img

Swap
1

img

Stop
1
keep repecting to 4th Iteration
1

# Finalize

img

# Python Implementation

arr = [564, 11, 300, 0, -90, 60, 60]

def bubble_sort(arr):
    for i in range(0,len(arr)):
        for j in range(0,len(arr)-1):
            if(arr[j]>arr[j+1]):
                arr[j],arr[j+1] = arr[j+1],arr[j]

bubble_sort(arr)
print(arr)
1
2
3
4
5
6
7
8
9
10

OUTPUT

[-90, 0, 11, 60, 60, 300, 564]