Max Comparisons in Binary Search: Understanding Efficiency and Implementation

Click For Summary
SUMMARY

The discussion focuses on the efficiency and implementation of binary search, specifically addressing two homework questions related to comparisons. For a binary search on a 2,000-element array, the maximum number of comparisons required is 11, derived from the formula log2(2000). After three comparisons, only 1/8 of the array remains to be searched, calculated as (1/2)^3. The conversation highlights the exponential nature of binary search and its logarithmic complexity.

PREREQUISITES
  • Understanding of binary search algorithm
  • Familiarity with logarithmic functions, specifically log2
  • Basic knowledge of C++ programming
  • Concept of exponential decay in search algorithms
NEXT STEPS
  • Study the implementation of binary search in C++
  • Learn about the time complexity of search algorithms
  • Explore the concept of logarithmic functions in depth
  • Investigate the differences between linear and binary search
USEFUL FOR

Students in introductory computer science courses, particularly those learning about algorithms and data structures, as well as educators teaching binary search concepts.

MinusTheBear
Messages
22
Reaction score
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
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   Reactions: Of Mike and Men
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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 46 ·
2
Replies
46
Views
9K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
5
Views
4K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K