I'm in a 200 level CS class called Data Structures and Algorithms, and I'm having a hard time understanding how to find the time complexity of an algorithm. I don't get how you look at an algorithm and decide if it's n, or log(n), or what.
I can usually tell that if something is just assignments, that's constant, and a loop is n, and a nested loop is n^2, n^3, etc.
But then there's something like this:
CODE
sum=0;
for(int j=1; j<n; j*=2){
sum++;
}
Apparently that's O(log(n)), but I have no idea where that comes from. My professor seems to expect that we can just look at an algorithm and just know what it's time complexity is.
And we had a quiz question recently that said, "For the typical algorithm that you use to perform calculations by hand, determine the running time in terms of n to a) add two n-digit numbers, and b ) multiply two n-digit numbers."
I said that adding would be constant, and multiplying would be n. But the right answer is that adding is n, and multiplying is n^2, and I don't understand why.
Is there some method by which I can figure this out?
Help appreciated, thanks!
This post has been edited by nebulinda: 11 Sep, 2009 - 01:33 PM