reset password
Author Message
Rinchan7
Posts: 56
Posted 13:47 Oct 02, 2014 |

How does one know whether or not an algorithm's running time might be O(log n) or O(n log n)? Based on the first 4 videos, I understand that log is the amount of times you have to divide by 2 to get to ~ 1, but I never really understood where the log comes from when finding the running time. How do you see that a step (or a group of steps) runs in (log n) or (n log n) time?

rabbott
Posts: 1649
Posted 14:05 Oct 02, 2014 |

There are log n levels in the recursion tree. At each level you have to do as many merges as there are sub-problems. At level j there are 2^j subproblems.  Each merge requires as many steps as are elements in the subproblem. At level j you are dealing with n/(2^j) elements to merge. So at each level you are doing (2^j) * (n/(2^j)) steps. The two (2^j) factors cancel each other out leaving n. In other words. At each level you have to do n steps.  (All this ignores constants.)  Since there are log n levels, the total is n * log n..

Last edited by rabbott at 14:05 Oct 02, 2014.
Rinchan7
Posts: 56
Posted 14:15 Oct 02, 2014 |

I think I got that, thanks