| Learn | Algorithm - Big-O Notation - An o log n example |
|---|
An O(log n) example IntroductionConsider the following problem:
Naïve approachThe first thing that comes to mind is to just read every index until 0 is found. In the worst case, the number of operations is This works fine for small values of DichotomyConsider the following algorithm (Python3):
In the worst case, we have to wait until ExplanationNote: When we write "log", we mean the binary logarithm, or log base 2 (which we will write "log_2"). As O(log_2 n) = O(log n) (you can do the math) we will use "log" instead of "log_2". Let's call x the number of operations: we know that 1 = n / (2^x). So 2^x = n, then x = log n ConclusionWhen faced with successive divisions (be it by two or by any number), remember that the complexity is logarithmic. |
Read Article
https://develop.horje.com/learn/1434/reference