ProgrammingHard

Bubble sort with an early exit when a pass makes no swap is given an already sorted array of 5 elements. How many comparisons does it make?

  1. A. 10
  2. B. 5
  3. C. 20
  4. D. 4

Correct answer: D. 4

The first pass compares 4 adjacent pairs, sees no swap and stops. The early exit makes a sorted input linear.

More C Programming questions

Exams that ask this topic