What is the base of logarithm on all time-complexity algorithms? Is it base 10 or base e?
When we say that the average sorting complexity is O(n log n). Is the base of log n 10 or e?
A common algorithm with O(log n) time complexity is Binary Search whose recursive relation is T(n/2) + O(1) i.e. at every subsequent level of the tree you divide problem into half and do constant amount of additional work.
Logarithmic time complexity log(n): Represented in Big O notation as O(log n), when an algorithm has O(log n) running time, it means that as the input size grows, the number of operations grows very slowly. Example: binary search.
Specifically, we'll use the Binary Search algorithm and its logarithmic time complexity – O(log n). Binary Search is an algorithm that is used to search for an element in an ordered set.
It depends where the logarithm is. If it is just a factor, then it doesn't make a difference, because big-O or θ allows you to multiply by any constant. If you take O(2logn) then the base is important.
In Computer Science, it's often base 2. This is because many divide and conquer algorithms that exhibit this kind of complexity are dividing the problem in two at each step.
Binary search is a classic example. At each step, we divide the array into two and only recursively search in one of the halves, until you reach a base case of a subarray of one element (or zero elements). When dividing an array of length n
in two, the total number of divisions before reaching a one element array is log2(n)
.
This is often simplified because the logarithms of different bases are effectively equivalent when discussing algorithm analysis.
In Big-O complexity analysis, it doesn't actually matter what the logarithm base is. (they are asymptotically the same, i.e. they differ by only a constant factor):
O(log2 N) = O(log10 N) = O(loge N)
Most of the time when mathematicians talk about logs, they implicitly mean to base e
. Computer Scientists would tend to favour base 2, but it doesn't actually matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With