Algorithms · Question 3 · Easy

Algorithms question 3

Bubble sort is implemented with a flag that stops the algorithm when a pass makes no swaps. What is its time complexity on an array that is already sorted?

  1. O(n²)
  2. O(n log n)
  3. O(n)
  4. O(1)
Answer and explanation

Answer: C. O(n)

Explanation: The first pass makes n - 1 comparisons and no swaps, so the flag ends the algorithm after one pass: O(n).

Previous: Which of the following sorting algorithms is NOT comparison-based? · Next: What is the maximum number of probes (middle-element checks) binary search…

Practise algorithms

Try a fresh set of algorithms questions with an explanation after every answer, free and without signing up: Algorithms practice. Or sit a full placement mock test.

More algorithms questions

More technical & programming practice

Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge