This preview shows pages 1–2. Sign up to view the full content.
2.4 A Survey of Common Running Times
14
Linear Time: O(n)
Linear time.
Running time is proportional to input size.
Computing the maximum.
Compute maximum of n numbers a
1
, …, a
n
.
max
(
a
1
for
i = 2 to n {
if
(a
i
> max)
max
(
a
i
}
15
Linear Time: O(n)
Merge.
Combine two sorted lists
A = a
1
,a
2
,…,a
n
with B = b
1
,b
2
,…,b
n
into sorted whole.
Claim.
Merging two lists of size n takes O(n) time.
Pf.
After each comparison, the length of output list increases by 1.
i = 1, j = 1
while
(both lists are nonempty) {
if
(a
i
#
b
j
) append a
i
to output list and increment i
else
(a
i
#
b
j
)
append b
j
to output list and increment j
}
append remainder of nonempty list to output list
16
O(n log n) Time
O(n log n) time.
Arises in divideandconquer algorithms.
Sorting.
Mergesort and heapsort are sorting algorithms that perform
O(n log n) comparisons.
Largest empty interval.
Given n timestamps x
1
, …, x
n
on which copies
of a file arrive at a server, what is largest interval of time when no
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 '08
 SHAHRIARSHAMSIAN

Click to edit the document details