Time complexity of a binary search

Click For Summary

Discussion Overview

The discussion revolves around the time complexity of binary search, specifically addressing whether it should be expressed as ##O(\log_2 n)## or ##O(\log n)##. Participants explore the implications of using different logarithmic bases in the context of algorithm analysis and notation conventions across various fields.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants assert that binary search eliminates half of the objects in a sorted array each time, suggesting that its time complexity should be ##O(\log_2 n)##.
  • Others question the significance of the difference between ##\log x## and ##\log_2 x##, noting that as input tends to infinity, the difference diminishes.
  • A participant explains that a multiplying constant is not important in big O notation, leading to the conclusion that ##O(\log n)## is equivalent to ##O(\log_2 n)##.
  • There is a discussion about how to express ##\log_b x## in terms of ##\log x##, with a participant providing the relationship ##\log_2 x = \frac{\log x}{\log 2}##.
  • Some participants clarify that the notation ##\log x## can vary by field, with computer science typically using base 2, while mathematics and physics often use base e, and chemistry may use base 10.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate notation for logarithmic time complexity and the implications of using different bases. There is no consensus on whether to prefer ##O(\log_2 n)## or ##O(\log n)##, as opinions vary based on disciplinary conventions.

Contextual Notes

Participants note that the interpretation of logarithmic notation can depend on the context, leading to potential ambiguity in discussions across different scientific fields.

YoungPhysicist
Insights Author
Messages
350
Reaction score
203
Binary search works by eliminating half of the objects in a sorted array every time,so shouldn’t it’s time complexity being ##O(\log_2 n)## instead of ##O(\log n)##?
 
Technology news on Phys.org
Where is the difference?
 
mfb said:
Where is the difference?
I do get that when the input tends to infinity,the difference between ##\log x## and ##\log_2 x## becomes smaller and smaller,but why don’t they just use ##O(\log_2 x)##?
 
Because a multiplying constant is of no importance in the O notation.
 
Young physicist said:
I do get that when the input tends to infinity,the difference between ##\log x## and ##\log_2 x## becomes smaller and smaller,but why don’t they just use ##O(\log_2 x)##?
It isn't about limits as n tends to infinity. Do you know how to express the ##\log_b x##, where ##b## is any base, in terms of ##\log x##?
 
Ibix said:
Do you know how to express the logbxlogb⁡x\log_b x, where bbb is any base, in terms of logxlog⁡x\log x?
Yes, I do.
$$\log_2 x = \frac{\log x}{\log 2}$$
 
So ln(x) and log2 only differ by a constant (ln(2)). A constant doesn't matter. ##O(\log n) = O(5\log n) = O(46342\log n)##
 
  • Like
Likes   Reactions: QuantumQuest and YoungPhysicist
mfb said:
So ln(x) and log2 only differ by a constant (ln(2)). A constant doesn't matter. ##O(\log n) = O(5\log n) = O(46342\log n)##
Ohh~~problem solved!

Edit:so this ##\log x## means ##\log_e x## rather than ##\log_{10} x## ?
 
Last edited:
Young physicist said:
Ohh~~problem solved!

Edit:so this ##\log x## means ##\log_e x## rather than ##\log_{10} x## ?
It doesn't matter. All bases do the same thing - you just multiply by a constant to change base, and you discard that constant in this case.
 
  • Like
Likes   Reactions: QuantumQuest and YoungPhysicist
  • #10
Ibix said:
It doesn't matter. All bases do the same thing - you just multiply by a constant to change base, and you discard that constant in this case.
Yeah.Just making sure the ln(x) posted by @mfb isn’t an accident.
 
  • #11
Young physicist said:
Binary search works by eliminating half of the objects in a sorted array every time,so shouldn’t it’s time complexity being ##O(\log_2 n)## instead of ##O(\log n)##?
In computer science books ##\log(n)## is usually taken to mean ##\log_2(n)##.

In the calculus textbooks I learned from, which was a while ago, ##\log(x)## meant ##\log_{10}(x)##, the so-called common logarithm. The notation ##\ln(x)## was reserved for the natural logarithm, or ##\log_e##. I'm told that more modern calculus textbooks treat ##\log## and ##\ln## as synonyms.
 
  • Like
Likes   Reactions: QuantumQuest
  • #12
Young physicist said:
Yeah.Just making sure the ln(x) posted by @mfb isn’t an accident.
I used ln(x) to avoid ambiguity.

Computer science read log(x) as base 2, mathematics and most of physics will see it as base e, chemistry will see it as base 10, ...
 
  • #13
mfb said:
Computer science read log(x) as base 2, mathematics and most of physics will see it as base e, chemistry will see it as base 10, ...
woah!:eek:
 

Similar threads

Replies
12
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
10
Views
5K