Which statement about time complexities is true for binary search compared to linear search?

Prepare for the 241 Computer Science Certification Exam with comprehensive flashcards and multiple choice questions. Enhance knowledge with explanations and hints to excel in your test journey!

Multiple Choice

Which statement about time complexities is true for binary search compared to linear search?

Explanation:
Binary search relies on halving the search space with each comparison, which leads to about log2(n) steps in the worst case. Because you can only safely discard half of the data if it’s sorted, this algorithm achieves O(log n) time when the data is in order. If the data isn’t sorted, that halving logic doesn’t reliably lead to the target, so you don’t get the log n efficiency without first sorting. Linear search, by contrast, may have to check every element, giving O(n) time. So the correct idea is that binary search runs in O(log n) time and requires sorted data.

Binary search relies on halving the search space with each comparison, which leads to about log2(n) steps in the worst case. Because you can only safely discard half of the data if it’s sorted, this algorithm achieves O(log n) time when the data is in order. If the data isn’t sorted, that halving logic doesn’t reliably lead to the target, so you don’t get the log n efficiency without first sorting. Linear search, by contrast, may have to check every element, giving O(n) time. So the correct idea is that binary search runs in O(log n) time and requires sorted data.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy