ElitmusZone » How to Calculate Running Time of an algorithm

How to Calculate Running Time of an algorithm

  1. If-then-else Statements

Here, either sequence 1 will execute, or sequence 2 will execute, Therefore, the worst case time is the slowest of two possibilities.

Hence, max(time(sequence 1), time(sequence 2))

For example, if sequence 1 is O(N) and sequence 2 is O(1) the worst-case time complexity for the whole if-then-else statement would be O(N).

  1. Loops

The loop executes N times, so sequence of Statements also executes N times.If we assume statements are of O(1). The total time for the for loop is N * O(1) which is O(N) overall.