The Question
How to find time complexity of an algorithm?
What have I done before posting a question on SO ?
I have gone through this, this and many other links
But no where I was able to find a clear and straight forward explanation for how to calculate time complexity.
What do I know ?
Say for a code as simple as the one below:
char h = 'y'; // This will be executed 1 time
int abc = 0; // This will be executed 1 time
Say for a loop like the one below:
for (int i = 0; i < N; i++) {
Console.Write('Hello World !');
}
int i=0; This will be executed only once.
The time is actually calculated to i=0
and not the declaration.
i < N; This will be executed N+1 times
i++ ; This will be executed N times
So the number of operations required by this loop are
{1+(N+1)+N} = 2N+2
Note: This still may be wrong, as I am not confident about my understanding on calculating time complexity
What I want to know ?
Ok, so these small basic calculations I think I know, but in most cases I have seen the time complexity as
O(N), O(n2), O(log n), O(n!).... and many other,
Can anyone help me understand how does one calculate time complexity of an algorithm? I am sure there are plenty of newbies like me wanting to know this.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…