Computer Science I / Searching Strategies
Practice question · Put in order

Order the 'invest structure to buy speed' workflow the lesson describes for searching a fixed dataset many times.

Hints
  1. The one-time cost is paid before any of the fast searches.
  2. Sorting first is what makes every later search logarithmic.
Show the answer
  1. Sort the dataset once, at a cost of about n log n
  2. For each query, jump to the middle of the sorted data
  3. Halve the range with each comparison
  4. Answer each search in about log n comparisons
Why

Pay the one-time n log n sort, then every subsequent search is O(log n) instead of O(n). If you search many times, that sort pays for itself, structure bought once, speed gained repeatedly.

Read the lesson: Searching Strategies →

Practise Searching Strategies

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

More questions on Searching Strategies