I follow the course of algorithms and data structures.
Today my professor said that the complexity of the following algorithm is 2 n .
I waited until the lesson was over, went up to him and told him that I really believed that it was an algorithm O(n)
, and I did the calculations to prove it, and wanted to show them that, but he kept talking about it, not letting me no convincing explanation.
The algorithm is recursive and has such complexity:
{ 1 if n=1
T(n) = {
{ 2T(n/2) otherwise
I calculated it like O(n)
this:
Let it expand T(n)
T(n) = 2 [2 * T(n/(2^2))]
= 2^2 * T(n/(2^2))
= 2^2 * [2 * T(n/(2^3))]
= 2^3 * T(n/(2^3))
= ...
= 2^i * T(n/(2^i)).
We stop when the term inside T is 1, that is:
n / (2 i ) = 1 ==> n = 2 i ==> i = log n
After substitution we get
T(n) = 2^log n * T(1)
= n * 1
= O(n).
Merge Sort, , Merge Sort, , , O(n log n)
, 2T (n/2) + Θ (n) (, , 2T (n/2)), , , , -O. . : " , , ".
:
, .