# Sorting algorithms
- There are many sorting sorting algorithms. For example, Bubble sort (opens new window), Merge sort (opens new window), Insertion sort (opens new window), Quicksort (opens new window), etc.
# Bubble sort
# Swapping in Python
a = 30
b = 90
print(a)
print(b)
1
2
3
4
2
3
4
OUTPUT
30
90
a = 30
b = 90
a,b = b,a
print(a)
print(b)
1
2
3
4
5
2
3
4
5
OUTPUT
90
30
# Bubble sort algorithm
# Initialize
1st Iteration
if 5 > 9 then Swap
1
if 9 > 3 then Swap
1
Swap
1
if 9 > 1 then Swap
1
Stop
1
2nd Iteration
if 5 > 3 then Swap
1
Swap
1
if 5 > 1 then Swap
1
Swap
1
Stop
1
keep repecting to 4th Iteration
1
# Finalize
# 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
2
3
4
5
6
7
8
9
10
OUTPUT
[-90, 0, 11, 60, 60, 300, 564]