Display (q)
-- preserves q
-- post: contents of q are displayed |
|
|
|
|
Executions |
|
s1 |
i ¬ 1 |
|
1 |
|
s2 |
while i ≤ length[q] |
|
N+1 |
|
s3 |
x ¬ Dequeue (q) |
|
N |
|
s4 |
print x |
|
N |
|
s5 |
Enqueue (q, x) |
|
N |
|
s6 |
i ¬ i + 1 |
|
N |
The size of the problem N depends on the number of items in the
queue.
N = length[q]
The first statement (s1) gets executed exactly 1 time no matter how many items
are in the queue.
- s1 contributes 1 * N0 instructions
to the total.
- Statement s2 gets executed N + 1 times.
Question: Why?
- Statements s3, s4, s5, and s6, get
executed N times
- So these statements contribute 4 * N1 instructions to
the total
f(N) = (1 * N0) + (N1 + 1) + (4 * N1)
= 5 * N1 + 1 * N0
= 5
* N1 + 1
= 5N + 1
This is is a linear function where the slope is 5 and the
y-intercept is 1.
In Big-O notation, this operation is referred to as an O(N)
operation or a linear time operation.
5N + 1 is O(N)