How do we actually find the complexity of a piece of code? We agree on a simple model of a computer, decide what one “step” costs, and then add up the steps statement by statement. This page gives the model and the rules for the common statement types.
Performance versus complexity
An algorithm can use several resources: CPU time (time complexity), memory (space complexity), disk input/output and network bandwidth. Two different questions can be asked about them:
Performance
Complexity
Question
How much time/memory does this program use in this run?
How does the requirement grow as the input grows?
Depends on
Algorithm, machine, compiler, input
Only the algorithm
Unit
Seconds, megabytes
A growth function such as O(n2)
Measured by
Running and timing the program
Analysing the algorithm
For example, if the working memory is O(n2), doubling n makes it roughly four times larger for large n, because (2n)2=4n2.
The RAM model
Common orders and what doubling n does
Order
Name
If n doubles, time …
O(1)
constant
stays the same
O(logn)
logarithmic
grows by one step
O(n)
linear
doubles
O(n2)
quadratic
becomes 4 times
O(n3)
cubic
becomes 8 times
Rules for statements
1. Sequence. The time of statements one after another is the sum of their times. If each is O(1), a fixed number k of them is still O(1).
2. If-then-else. Only one branch runs, but in the worst case it is the slower one:
Pattern: if-then-else
if (condition) -> O(1) for the test sequence 1 -> say O(n^2)else sequence 2 -> say O(1)worst case = test + slower branch = O(n^2)
3. Simple loop. Time = (number of iterations) × (time of the body). A loop that runs n times with an O(1) body is O(n). How many statements are in the body does not matter, as long as the number is fixed.
4. Nested loops. Work from the inside out and multiply. An outer loop that runs n times around an inner loop that runs m times executes the inner body n⋅m times: O(n⋅m), or O(n2) when both run n times.
Loops that double or halve
Not every loop counts up by 1. If the loop variable is multiplied by 2 each time, it reaches n very quickly.
Key takeaways
Performance is measured on a machine; complexity is a property of the algorithm.
In the RAM model every simple operation and memory access costs one step.
Sequence: add; if-else: take the worse branch; loop: iterations × body; nested loops: multiply.
A doubling or halving loop runs ⌊log2n⌋+1 times: O(logn).
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Find the time complexity of for (i = 1; i <= n; i *= 2) j = 1; and justify it.
Show worked answer
The assignment j = 1 is O(1). The loop variable takes the values 1,2,4,…,2k while 2k≤n, so k runs from 0 to ⌊log2n⌋: the body executes ⌊log2n⌋+1 times. Total time =(⌊log2n⌋+1)⋅O(1)=O(logn).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/analysis-of-algorithms/the-process-of-analysis-and-the-ram-model/ to read this page.