How did the logn
got into the complexity formula?
- For each step, you invoke the algorithm recursively on the first and second half.
Thus - the total number of steps needed, is the number of times it will take to reach from n
to 1
if you devide the problem by 2 each step.
So you are actually looking for a k
such that:
n / 2 /2 / 2 / ... /2 = 1
^
(k times)
But, note that the equation is actually: n / 2^k = 1
. Since 2^logn = n
, we get k = logn
. So the number of steps (iterations) the algorithm requires is O(logn), which will make the algorithm O(nlogn)
- since each iteration is O(n)
.
Note: The complexity in here is not exact, quicksort in rare cases decays to O(n^2)
, it is depends on the pivot selection. The "devide the problem by 2 each step" is a simplification, but it does not change the average analyzis of the algorithm.
Why use big O notation?
It is simple and platform independent.
The exact number of op (and sometimes even comparisons) is platform dependent. (If instruction set A is richer then instruction set B, it will probably need more ops).
It is definetly not the only method used. For real life applications, the exact run time (in seconds) is very important factor and is often used.
So, in short - big O notation gives us easy to calculate - platform independent approximation on how will the algorithm behave asymptotically (at infinity), which can divide the "family" of algorithm into subsets of their complexity, and let us compare easily between them.
Also, other notations that are used are small o, theta and big/small omega.