Computer Science

Time Complexity Bubble Sort

Describe time complexity of bubble sort.

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

Formula Explanation

Bubble sort repeatedly swaps adjacent elements if they are in wrong order.

Variables Explained

n = number of elements.

Solved Example

Worst case comparisons for 5 elements?
Approximately 10 comparisons (n(n-1)/2).

Exam Tip

Best case occurs when array is already sorted.