What is the time complexity of binary search and what data property does it require?

Get ready for your Fundamentals of Computing Test. Utilize flashcards and multiple-choice questions. Every question includes hints and explanations. Prepare effectively and ace your exam now!

Multiple Choice

What is the time complexity of binary search and what data property does it require?

Explanation:
Binary search relies on the data being ordered, so you can safely discard half of the remaining elements after each comparison. You compare the target to the middle element: if the target is smaller, you continue in the left half; if larger, in the right half. Each step cuts the search space roughly in half, which means the number of steps grows logarithmically with the number of elements. That's why the time complexity is O(log n). If the data isn’t sorted, you can’t guarantee that halving the search space is valid, so the method loses its efficiency and may degrade toward a linear scan. So the best description is O(log n) time with the data being sorted.

Binary search relies on the data being ordered, so you can safely discard half of the remaining elements after each comparison. You compare the target to the middle element: if the target is smaller, you continue in the left half; if larger, in the right half. Each step cuts the search space roughly in half, which means the number of steps grows logarithmically with the number of elements. That's why the time complexity is O(log n). If the data isn’t sorted, you can’t guarantee that halving the search space is valid, so the method loses its efficiency and may degrade toward a linear scan. So the best description is O(log n) time with the data being sorted.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy