Bubble Sort Python (original) (raw)

Last Updated : 14 Nov, 2025

Bubble Sort is one of the simplest sorting algorithms. It repeatedly compares adjacent elements in the list and swaps them if they are in the wrong order.

How Bubble Sort Works

  1. Compare each pair of adjacent elements.
  2. If the first element is greater than the second, swap them.
  3. After each full pass, the largest unsorted element "bubbles up" to its correct position at the end.
  4. Repeat this process for the remaining unsorted portion of the list.
  5. The algorithm stops early if no swaps occur in a pass, meaning the array is already sorted.

Python Implementation

Python `

def bubbleSort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swapped = True if not swapped: break

arr = [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print(arr)

`

Output

[11, 12, 22, 25, 34, 64, 90]

**Explanation: