What is the time complexity of Bubble Sort?

Quick Overview

Give the best, average and worst-case time complexity of bubble sort and explain where each bound comes from. It tests counting comparisons and swaps, what the best case depends on, and how bubble sort compares with other simple sorts.

What is the time complexity of Bubble Sort?

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

What is the time complexity of Bubble Sort? Give the best, average and worst cases and explain where each one comes from. ```hint Count the comparisons Count how many comparisons one pass makes and how many passes can run. Then check what the algorithm has to do to stop early. ``` ### What a Strong Answer Covers - The worst-case and average-case bounds, with the comparison count that produces them. - The best case, and the detail of the implementation it depends on. - The extra space used and whether the sort is stable. - How the number of swaps relates to how unsorted the input is. - How bubble sort compares with other simple quadratic sorts in practice. ### Follow-up Questions - How many swaps does bubble sort make on a given input, and how could you compute that number without running the sort? - Why is insertion sort usually preferred over bubble sort when both are O(n^2)? - Which nearly sorted input still forces an early-exit bubble sort to run every pass? - If each pass stops at the position of the previous pass's last swap, does the worst case change?

Overview: Give the best, average and worst-case time complexity of bubble sort and explain where each bound comes from. It tests counting comparisons and swaps, what the best case depends on, and how bubble sort compares with other simple sorts.

PracHub logo
General
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

What is the time complexity of Bubble Sort? Give the best, average and worst cases and explain where each one comes from.

What a Strong Answer Covers Guidance

  • The worst-case and average-case bounds, with the comparison count that produces them.
  • The best case, and the detail of the implementation it depends on.
  • The extra space used and whether the sort is stable.
  • How the number of swaps relates to how unsorted the input is.
  • How bubble sort compares with other simple quadratic sorts in practice.

Follow-up Questions Guidance

  • How many swaps does bubble sort make on a given input, and how could you compute that number without running the sort?
  • Why is insertion sort usually preferred over bubble sort when both are O(n^2)?
  • Which nearly sorted input still forces an early-exit bubble sort to run every pass?
  • If each pass stops at the position of the previous pass's last swap, does the worst case change?