Bubble Sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process continues until the list is fully sorted. While it’s easy to understand, Bubble Sort is not very efficient for large datasets due to its quadratic time complexity. It’s often used for educational purposes or as a baseline for comparison with other sorting algorithms.
for i = 1 to (n - 1):
swapped = false
for j = 1 to (n - i):
if arr[j] < arr[j + 1]:
swap(j, j + 1)
swapped = true
if not swapped:
break