Binary search math, log base 2 (n) issues

Click For Summary

Homework Help Overview

The discussion revolves around the binary search algorithm and its time complexity, specifically the O(log2(n)) operations required in the worst case. Participants are examining the mathematical reasoning behind this complexity in the context of a specific example involving a set of numbers.

Discussion Character

  • Conceptual clarification, Mathematical reasoning, Assumption checking

Approaches and Questions Raised

  • Participants are attempting to understand why the number of operations in binary search is expressed as O(log2(n)). They present a specific example with a set of 7 elements and question the calculation of operations needed to find a specific number.

Discussion Status

There is ongoing clarification regarding the interpretation of the O notation and the actual number of operations required. Some participants are exploring the implications of rounding logarithmic values and whether the worst-case scenario aligns with their calculations.

Contextual Notes

Participants are addressing a potential misunderstanding about the number of elements in the set and the implications of asymptotic notation in relation to the actual number of steps taken in the binary search process.

ducmod
Messages
86
Reaction score
0

Homework Statement


Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.

Homework Equations


For example:
Find number 8 in the set 1234578.

The Attempt at a Solution


The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.

Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.

I don't see why it is log2(n).
Thank you!
 
Physics news on Phys.org
ducmod said:

Homework Statement


Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.

Homework Equations


For example:
Find number 8 in the set 1234578.

The Attempt at a Solution


The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.

Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.

I don't see why it is log2(n).
Thank you!
See https://en.wikipedia.org/wiki/Binary_search_algorithm#Performance
 
ducmod said:

Homework Statement


Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.

Homework Equations


For example:
Find number 8 in the set 1234578.

The Attempt at a Solution


The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.

Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.

I don't see why it is log2(n).
Thank you!

Why would you say the set {1,2,3,4,5,6,7,8} has seven elements? Count them: there are eight of them!
 
Last edited:
Ray Vickson said:
Why would you say the set {1,2,3,4,5,6,7,8} has seven elements? Count them: there are eight of them!
Ray, please, take a look at my example, it has 7 elements :) I don't have 6 in the set :)
 
ducmod said:

Homework Statement


Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.

Homework Equations


For example:
Find number 8 in the set 1234578.

The Attempt at a Solution


The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.

Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.

I don't see why it is log2(n).
Thank you!
You seem not to understand what the O notation means. If x is a variable that can become very large then O(x) is the same as O(x+1) etc.
 
ducmod said:

Homework Statement


Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.

Homework Equations


For example:
Find number 8 in the set 1234578.

The Attempt at a Solution


The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.

Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.

I don't see why it is log2(n).
Thank you!

The ##\log_2(n)## count is an asymptotic, worst-case bound. For ##n = 7## we have ##\log_2(7) \doteq 2.80735##, and no algorithm can take exactly that many steps.
 
ducmod said:
Ray, please, take a look at my example, it has 7 elements :) I don't have 6 in the set :)


Right: you got me on that. Good one!
 
Ray Vickson said:
The ##\log_2(n)## count is an asymptotic, worst-case bound. For ##n = 7## we have ##\log_2(7) \doteq 2.80735##, and no algorithm can take exactly that many steps.
Does it mean that I have to round numbers like 2.80735, and thus get the answer 3? So, the worst case scenario will require 3 steps when n = 7, correct?
 
haruspex said:
You seem not to understand what the O notation means. If x is a variable that can become very large then O(x) is the same as O(x+1) etc.

ducmod said:
Does it mean that I have to round numbers like 2.80735, and thus get the answer 3? So, the worst case scenario will require 3 steps when n = 7, correct?

See https://en.wikipedia.org/wiki/Big_O_notation.
 
  • #10
ducmod said:
Does it mean that I have to round numbers like 2.80735, and thus get the answer 3? So, the worst case scenario will require 3 steps when n = 7, correct?

No: 2.807 is an upper bound on the number of steps, and since the actual number of steps must be an integer, we must have N(steps) ≤ 2.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
3K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 11 ·
Replies
11
Views
5K
Replies
2
Views
3K
Replies
12
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 27 ·
Replies
27
Views
4K