Time complexity of a binary search

In summary, the conversation discusses the time complexity of binary search and whether it should be denoted as ##O(\log_2 n)## or ##O(\log n)##. The difference between the two notations is explained, with emphasis on the fact that a multiplying constant is of no importance in the O notation. The conversation also touches on the use of ln(x) and log(x) as synonyms, with a mention of how different fields may interpret the notation in different ways.
  • #1
YoungPhysicist
Insights Author
350
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
  • #3
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)##?
 
  • #4
Because a multiplying constant is of no importance in the O notation.
 
  • #5
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##?
 
  • #6
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}$$
 
  • #7
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 QuantumQuest and YoungPhysicist
  • #8
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:
  • #9
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 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 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:
 

What is the time complexity of a binary search?

The time complexity of a binary search is O(log n), where n is the number of elements in the sorted array.

How is the time complexity of a binary search calculated?

The time complexity of a binary search is calculated by counting the number of comparisons made in the worst case scenario, which is when the target element is not present in the array. This number of comparisons can be expressed as log2(n), where n is the number of elements in the array.

What is the difference between time complexity and space complexity in a binary search?

Time complexity refers to the number of operations or comparisons needed to complete a task, while space complexity refers to the amount of memory required to perform the task. In a binary search, the time complexity is O(log n) while the space complexity is O(1) as it only requires a constant amount of memory for the variables used.

What is the best case scenario for the time complexity of a binary search?

The best case scenario for the time complexity of a binary search is O(1), when the target element is the middle element of the array. In this case, only one comparison is needed to find the target element.

What is the worst case scenario for the time complexity of a binary search?

The worst case scenario for the time complexity of a binary search is O(log n), when the target element is not present in the array and the search has to go through all the elements in the array before determining that the target element is not present. This is because the search has to continually divide the array in half to determine which half the target element may be in.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
596
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
Replies
13
Views
2K
Back
Top