Skip to main content

Bubble sort

Bubble sort is an algorithm to order an array by repeatedly scanning it while moving numbers largest than their predecessor by swapping them.

Complexity analysis

  1. Space complexity
    • O(1)\mathcal{O}(1)
  2. Time complexity
    • O(n2)\mathcal{O}(n^2)

Pseudocode

Algorithm BubbleSort(a, n)

1: for i := n to 1 do
2: for j := 1 to i - 1 do
3: if a[j] > a[j + 1]
4: t := a[j]; a[j] := a[j + 1]; a[j + 1] := t;
5: return a;