Max Comparisons in Binary Search: Understanding Efficiency and Implementation

In summary, after three comparisons, the binary search function will only search for 1/2 of the array.
  • #1
MinusTheBear
22
0
I have two questions over linear and binary serch.

1. Homework Statement

Q1: In binary search, after three comparisons have been made, only ___ of the array will be left to search.
Q2: The maximum number of comparisons that a binary search function will make when searching for a value in a 2,000-element array is ___.

Homework Equations


n/a

The Attempt at a Solution


Q1: I know that 2n = # of elements, where n is the maximum number of comparisons required; or that log2(# of elements) = n

I'm assuming that you're using n = 3, but I'm not sure how the book gets the answer of 1/8.

Q2: I know the answer here is 11 based on log2(# of elements) = n, where n is the maximum number of comparisons. My question is, however, is it always ceil(n)? It would make sense since were dealing with a maximum but the book doesn't say other than that it's an exponential function.

This is an intro course to C++ so I'm sure that's why they don't go super in depth.
 
Physics news on Phys.org
  • #2
MinusTheBear said:
I have two questions over linear and binary serch.

1. Homework Statement

Q1: In binary search, after three comparisons have been made, only ___ of the array will be left to search.
Q2: The maximum number of comparisons that a binary search function will make when searching for a value in a 2,000-element array is ___.

Homework Equations


n/a

The Attempt at a Solution


Q1: I know that 2n = # of elements, where n is the maximum number of comparisons required; or that log2(# of elements) = n

I'm assuming that you're using n = 3, but I'm not sure how the book gets the answer of 1/8.

How much is left after the first comparison? After the second? The third? You don't need logs or exponents to figure this one out.
 
  • Like
Likes Of Mike and Men
  • #3
LCKurtz said:
How much is left after the first comparison? After the second? The third? You don't need logs or exponents to figure this one out.
ahhh gotcha. It'd be (1/2)3. Thanks
 

FAQ: Max Comparisons in Binary Search: Understanding Efficiency and Implementation

1. What is the difference between linear and binary search?

Linear search involves checking each element in a list or array one by one until the desired element is found. Binary search, on the other hand, involves dividing the list into halves and eliminating one half based on whether the desired element is greater or less than the midpoint. This process is repeated until the element is found.

2. Which search algorithm is more efficient, linear or binary search?

Binary search is typically more efficient than linear search, as it has a time complexity of O(log n) compared to linear search's O(n) time complexity. This means that binary search can find an element in a sorted list of n elements in log n time, while linear search would take n steps to find the same element.

3. When should I use linear search and when should I use binary search?

Linear search is suitable for smaller lists or when the list is not sorted, as it does not require the list to be in any particular order. Binary search is more suitable for larger lists that are sorted, as it can significantly reduce the number of comparisons needed to find an element.

4. Can binary search be used on unsorted lists?

No, binary search can only be used on sorted lists. If the list is unsorted, the results of binary search will be incorrect. In this case, linear search would be a more appropriate choice.

5. What is the worst-case time complexity for linear and binary search?

The worst-case time complexity for linear search is O(n), meaning that the desired element is the last element in the list or is not present at all. The worst-case time complexity for binary search is O(log n), meaning that the desired element is either the first or last element in the list or is not present at all.

Back
Top