ProgrammingHard

Bubble sort with a swapped flag stops early. On an already sorted array of length n, how many passes run?

  1. A. n passes, which is O(n^2)
  2. B. One pass, with no swaps, which makes the best case O(n)
  3. C. log n passes
  4. D. Zero passes, since the array is already sorted

Correct answer: B. One pass, with no swaps, which makes the best case O(n)

The first pass makes no swaps, so the flag is clear and the loop ends. The best case is linear.

More C Programming questions

Exams that ask this topic