Example: Measuring Time Complexity of a Single Loop Algorithm
Explore how to measure the time complexity of a single loop algorithm by counting primitive operations in JavaScript. Understand how initialization, increment, and test operations contribute to overall running time and learn to compute the algorithm's time complexity accurately.
We'll cover the following...
A For Loop With n Iterations
Now, let’s consider what happens when a loop is involved. Consider the following ...
Let’s count the number of primitive operations in the above program. We skip the non-executable lines and come to lines 2 and 3 where variable initializations are taking place, which account for one primitive operation, each.
Line 6 is a loop statement. To count the number of primitive operations on that line, we must dissect it into its constituents: the initialization, the increment, and the test. The initialization occurs only once and is counted as one primitive ...