Back to Topic 6.4 — Programming algorithms
6.4.2Computer Science SL5 flashcards

Linear and binary search

Practice Flashcards

Flip to reveal answers
Card 1 of 56.4.2
6.4.2
Question

How does linear search work, and what does it cost?

Click to reveal answer

Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.

All 5 Flashcards — Linear and binary search

Sign up free to track progress and get spaced-repetition review schedules.

Card 1process

Question

How does linear search work, and what does it cost?

Answer

It checks each item in turn until it finds the target or runs out, returning the position or -1. Best case O(1) if the target is first, worst case O(n) if it is last or absent.

Card 2process

Question

How does binary search work?

Answer

Look at the middle item. If it equals the target, stop. If it is too small the target is to the right, so move low to mid + 1; if too big, move high to mid - 1. Each step discards about half the remaining list.

Card 3concept

Question

Why must binary search have sorted data?

Answer

Discarding half depends on knowing which half the target would be in, which only holds if the list is in order. On unsorted data it does not run slowly — it returns the wrong answer.

Card 4concept

Question

How does binary search terminate?

Answer

When low passes high, meaning there is nothing left to search. It then returns -1 to indicate the target is absent.

Card 5comparison

Question

How do the two searches grow with the data?

Answer

Doubling the list doubles linear search's worst case, but adds only one comparison to binary search's. Sixteen items need at most four binary comparisons; a million need about twenty.

Track your progress with spaced repetition

Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.

Start Free
IB Computer Science Linear and binary search Flashcards | 6.4.2 | Aimnova