Bubble Sort

Speed

About algorithm

Explanation

Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until no more swaps are needed.

Steps

  1. Start at the beginning of the array.
  2. Compare each pair of adjacent elements.
  3. If they are in the wrong order, swap them.
  4. Continue to the end of the array (one pass).
  5. Repeat the process for all elements until the array is sorted.

Pseudocode

bubbleSort(arr):
  n = len(arr)
  for i from 0 to n - 1:
    for j from 0 to n - i - 1:
      if arr[j] > arr[j + 1]:
        swap arr[j] and arr[j + 1]

Complexity

Time: O(n²)

Space: O(1)