Algorithm Learning/Bubble Sort

Bubble Sort

Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

EasySortingBasicsComparison SortO(n²)

Definition

Bubble sort is a sorting algorithm that compares adjacent elements and moves the larger value backward, like bubbles rising to the surface.

Key Characteristics

  • Compares only adjacent elements - Very simple to implement and understand
  • Largest value moves to the end in each round
  • Stable Sort - Preserves relative order of equal elements
  • O(n²) time complexity - Inefficient for large datasets

Use Cases

Used in these scenarios:

📚

Algorithm Education

Best suited for educational purposes to learn basic sorting concepts

🔢

Small Dataset Sorting

Can be used simply for sorting small arrays with 10 or fewer elements

Nearly Sorted Data

Efficient for final cleanup of nearly sorted data

Operations

Main operations:

Compare

O(1)

Compare the size of two adjacent elements.

Swap

O(1)

Swap positions if the left value is larger.

Complexity

Time Complexity

Best
O(n)
Average
O(n²)
Worst
O(n²)

Space Complexity

O(1)

Understand Deeper with Visualization

See how the algorithm works through step-by-step animations and code execution.

Start Visualization