Skip to main content

Best, average, worst case of sorts

Short answer

Bubble sort: O(n) best (with flag), O(n^2) average/worst. Insertion sort: O(n) best, O(n^2) average/worst. Merge sort: O(n log n) all cases. Selection sort: O(n^2) all cases.

Provide best case, average case and worst case for the following algorithms in Big-Oh: bubble sort, insertion sort, merge sort and selection sort.

AlgorithmBest caseAverage caseWorst case
Bubble sortO(n) (with swap-flag, already sorted)O(n²)O(n²)
Insertion sortO(n) (already sorted)O(n²)O(n²) (reverse order)
Merge sortO(n log n)O(n log n)O(n log n)
Selection sortO(n²)O(n²)O(n²)

Without the early-exit flag, bubble sort is O(n²) even in the best case. Selection sort always scans the full unsorted part, so it never improves. Merge sort halves the list each time (log n levels) and merges in O(n) per level.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 7 questions →