Alorithms · Class 12 · CBSE · Science Computer Science

Explain why binary search cannot generally be applied directly to an unsorted array. Also state the worst-case time complexity of linear search and b…

Practice question from Alorithms (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · Subjective · Q-1AA8E4

Explain why binary search cannot generally be applied directly to an unsorted array. Also state the worst-case time complexity of linear search and binary search for an array of n elements.

Answer: Binary search depends on the array being sorted because it uses the middle element to discard either the left half or the right half of the remaining elements; this decision is not valid for an unsorted array. The worst-case time complexity of linear search is O(n), while that of binary search on a sorted array is O(log n).

Solution

Binary search can eliminate half the remaining elements at each step only when the array is sorted. Linear search may inspect every element, giving O(n) time, whereas binary search repeatedly halves the search space, giving O(log n) time.

More from Alorithms

All of Alorithms

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed