Mathematics I / Computational Complexity
Practice question · Multiple choice

Binary search is O(log n) and needs sorted data; a linear scan is O(n) and needs nothing. For a list searched once, which wins?

Hints
  1. Add up the total cost of each approach for one search, including any preparation.
  2. Ask how many searches you would need before the sort pays for itself.
Show the answer

B. The linear scan, since sorting first costs more than it saves

Why

Sorting to save one scan is a loss: O(n log n) to avoid O(n). The crossover comes at roughly log n searches, after which the preprocessing is repaid many times, which is the same calculation that decides whether a database should build an index or just scan the table.

Read the lesson: Computational Complexity →

Practise Computational Complexity

The app has 5 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.

More questions on Computational Complexity