Skip to main content

Bubble Sort

Repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order.

Interactive Visualization

Loading visualization…

Usage

src/sample/bubblesort.ts
import { bubbleSort } from 'athro';

const arr = [2, 6, 3, 8];
const result = bubbleSort(arr);

Time Complexity

CaseTime ComplexityDescription
BestO(n)When the array is already sorted
AverageO(n²)Pairwise comparisons across passes
WorstO(n²)Reverse-sorted input