Bubble Sort

A simple sorting algorithm that repeatedly swaps adjacent elements.

Time Complexity

O(n^2)

Slow

Space Complexity

O(1)

Immediate

In-Place

Yes

Yes

Stability

Yes

Yes

Difficulty

Easy

★★☆☆☆

Description

Bubble Sort is a straightforward sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

The algorithm gets its name because smaller elements bubble to the top of the list with each pass.

Steps:

  1. Compare adjacent elements
  2. Swap if needed
  3. Repeat until the list is sorted

While it is easy to understand and implement, Bubble Sort is inefficient on large datasets. However, it can be optimized to detect if the array is already sorted.

Computational Complexity

O(n)
Best
O(n^2)
Average
O(n^2)
Worst
O(1)
Spatial

Implementation