What is the main advantage of using binary search over linear search?
Binary search is much faster because it repeatedly divides the search space in half, reducing the number of comparisons from linear (n) to logarithmic (log n).
Click to reveal answer
beginner
Why must the data be sorted to use binary search?
Binary search relies on the data being sorted so it can decide which half of the data to ignore after each comparison, ensuring the search space shrinks correctly.
Click to reveal answer
beginner
In binary search, what happens if the middle element is less than the target value?
The search continues in the right half of the array because all elements to the left are smaller and can be ignored.
Click to reveal answer
beginner
How does sorted order help in efficient searching?
Sorted order allows algorithms like binary search to quickly eliminate half of the remaining elements at each step, making search operations much faster.
Click to reveal answer
beginner
What is the time complexity of binary search and why?
The time complexity is O(log n) because the search space is halved with each comparison, drastically reducing the number of steps needed compared to linear search.
Click to reveal answer
Why do we need sorted data for binary search?
ATo store data efficiently
BTo decide which half to search next
CTo avoid duplicates
DTo make data look neat
✗ Incorrect
Binary search uses the sorted order to decide whether to search the left or right half after comparing with the middle element.
What is the worst-case time complexity of binary search?
AO(n)
BO(n log n)
CO(log n)
DO(1)
✗ Incorrect
Binary search cuts the search space in half each time, leading to a logarithmic time complexity.
If the middle element is greater than the target, where does binary search continue?
ARight half
BStops immediately
CBoth halves
DLeft half
✗ Incorrect
Since the middle element is greater, the target can only be in the left half of the sorted array.
Which of these is NOT a benefit of sorted data?
AFaster insertion at any position
BEasier to find minimum and maximum
CFaster searching
DSupports binary search
✗ Incorrect
Insertion in sorted data can be slower because elements may need to be shifted to keep order.
What happens if you try binary search on unsorted data?
AIt may give wrong results
BIt works correctly
CIt runs faster
DIt sorts the data first
✗ Incorrect
Binary search assumes sorted data; without it, the search decisions are invalid and results can be wrong.
Explain why binary search requires sorted data and how it uses this property to find an element.
Think about how knowing order helps skip parts of the data.
You got /4 concepts.
Describe the main advantage of binary search compared to linear search and what sorted order enables.
Focus on speed and how sorted data helps.
You got /4 concepts.
Practice
(1/5)
1. Why is binary search only applicable on a sorted list?
easy
A. Because binary search works on unsorted lists by checking every element
B. Because binary search can only work on lists with unique elements
C. Because binary search divides the list and compares middle elements, which only works if the list is sorted
D. Because binary search requires the list to be reversed
Solution
Step 1: Understand binary search method
Binary search works by repeatedly dividing the list and comparing the middle element to the target.
Step 2: Importance of sorted order
This division and comparison only make sense if the list is sorted, so we know which half to discard.
Final Answer:
Because binary search divides the list and compares middle elements, which only works if the list is sorted -> Option C
Quick Check:
Sorted list needed for binary search = True [OK]
Hint: Binary search needs sorted data to decide halves quickly [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Believing binary search requires unique elements
Confusing binary search with linear search
2. Which of the following Rust code snippets correctly declares a sorted vector for binary search?
easy
A. let v = vec![1, 2, 3, 4];
B. let v = vec![3, 1, 4, 2];
C. let v = vec![4, 3, 2, 1];
D. let v = vec!["apple", "banana", "cherry"];
Solution
Step 1: Identify sorted vector
Binary search requires the vector to be sorted in ascending order. let v = vec![1, 2, 3, 4]; is sorted ascending: 1, 2, 3, 4.
Step 2: Check other options
let v = vec![3, 1, 4, 2]; is unsorted. let v = vec![4, 3, 2, 1]; is sorted descending, which is not standard for binary search. let v = vec!["apple", "banana", "cherry"]; is sorted alphabetically but contains strings, not integers.
Final Answer:
let v = vec![1, 2, 3, 4]; -> Option A
Quick Check:
Sorted vector for binary search = let v = vec![1, 2, 3, 4]; [OK]