Linear and Binary Search · Programming, Data Structures and Algorithms
3 questions · asked in 3 of 3 years (2024–2026)
MCQ 1 · MSQ 1 · NAT 11-mark 2 · 2-mark 1
2024Questions per year2026
Questions 1–3 of 3
Let A be a sorted array containing distinct integers. You perform a recursive binary search on to find an element . Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than .
The maximum number of comparisons that may have to be performed if is not an element of is ________. (Answer in integer)
The maximum number of comparisons that may have to be performed if is not an element of is ________. (Answer in integer)
Answer: 10
Binary search on distinct elements cuts the search space in half after every comparison. In the worst case, the number of comparisons needed is (or the same thing written as ).
Here, , so .
Answer: 10
Here, , so .
Answer: 10
For which of the following inputs does binary search take time in the worst case?
One or more options are correct.
Answer: (C)
Binary search reaches a worst-case time of only if it can go straight to the middle element in time at each step. This needs two things: (a) random access by index, which rules out linked lists because reaching the middle there takes traversal time, and (b) sorted order, which binary search needs to work correctly.
Only "an array of integers in increasing order" has both of these.
Answer: An array of n integers in increasing order
Only "an array of integers in increasing order" has both of these.
Answer: An array of n integers in increasing order
Let denote the maximum number of comparisons made while searching for an entry in a sorted array of size using binary search.
Which ONE of the following options is TRUE?
Which ONE of the following options is TRUE?
Answer: (A)
In each step, binary search compares the key with the middle element. That is 1 comparison. If the key is not the middle element, only one of the two parts is searched further.
After removing the middle element, the other elements are split into parts of size and . For the worst case we take the larger part, and . So
Why the other options are wrong:
- (B) would mean searching both halves. Binary search searches only one.
- (C) leaves out the comparison made at each step.
- (D) shrinks the array by only 1 element each time, which is linear search, not binary search.
Answer: (A)
After removing the middle element, the other elements are split into parts of size and . For the worst case we take the larger part, and . So
Why the other options are wrong:
- (B) would mean searching both halves. Binary search searches only one.
- (C) leaves out the comparison made at each step.
- (D) shrinks the array by only 1 element each time, which is linear search, not binary search.
Answer: (A)
Keep practising
Topics in Programming, Data Structures and Algorithms
Programming in PythonStacks, Queues and Linked ListsTreesHash Tables
Linear and Binary SearchYou are here
Sorting and Divide and ConquerGraph Theory and Graph Algorithms