Exercise 4.3 has you compute the actual number of
multiplications done by the third version of
mod-expt given an exponent.
> (mod-expt-mults signing-exponent)
984
The dramatic speedup is due to the fact that
at each step
mod-expt divides the exponent
in half.
Q: If
e is the exponent, then how is the
number of multiplications expressed in
big-Theta notation?
-
A: Θ(log e) instead of Θ(e)
The third version of mod-expt is an example of logarithmic
recursion.
Q: How is the amount of memory required by
mod-expt expressed in big-Theta
notation?
-
A: Θ(log e)
Objective: Count how many times cards are handled when you
sort
n cards. (Call this
H(n))
- First pass: All n cards are handled one or two times
- Second pass: n-1 cards are handled one or two times
- Third pass: n-2 cards are handled one or two times
- ...
-
n + (n-1) + (n-2) + ... + 1
is at most n2 and at least ¼n2
(n/2 of the numbers are n/2 or larger). So:
-
¼n2 ≤ H(n) ≤ 2n2
(Factor of 2 on the upper bound because each card is handled one or
two times.)
Count the number of card handlings:
- First pass: All n cards are handled once to create
n/2 stacks
- Second pass: All n cards are handled once to create
n/4 stacks
- Third pass: All n cards are handled once to create
n/8 stacks
- ...
- Each pass takes a
certain number of stacks and cuts it in half, doubling the
size of each stack.
Q: How many passes are required?
Pass |
Number of stacks |
Size of each stack |
1 |
n |
1 = 20 |
2 |
n/2 |
2 = 21 |
3 |
n/4 |
4 = 22 |
4 |
n/8 |
8 = 23 |
... |
... |
... |
p |
1 |
n = ? |
p |
1 |
n = 2p-1 |
-
n = 2p-1
-
log2n = log22p-1
-
log2n = p-1
-
p = log2n + 1
-
So the number of passes required by a merge sort of n numbers is
log2n + 1.
Since
n cards are handled on each of
log2n + 1
passes, the total number of handlings
H(n) is:
-
H(n) |
= |
n(log2n + 1) |
|
= |
nlog2n + n |
-
Since n ≤ nlog2n, then
H(n) |
≤ |
nlog2n + nlog2n |
|
≤ |
2nlog2n |
-
But H(n) ≥ nlog2n, so
-
H(n) is bounded by constant multiples
of nlog2n. (The constant for the lower bound
is 1; the constant for the upper bound is 2.)
Thus the time it takes to merge sort n cards is:
Θ(nlog2n)
-
This can also be expressed without a logarithmic base as:
Θ(nlogn)
since a number's logarithms of different bases differ only by a constant
factor (logca = logba × logcb).
Comparing n, nlogn, and n2
The chart below shows the functions
n (red),
nlogn (purple),
and
n2 (green) with the horizontal axis extending
to
n = 10.
Here are the values of the functions for
n = 10:
For larger
n the difference in the functions is more dramatic.
The chart to the right shows the functions
n (red),
nlogn (purple),
and
n2 (green) with the horizontal axis extending
to
n = 100, with
n2 = 10,000.
Note the difference between the general shapes (that is, the "
orders of
growth") of this graph and the preceding one:
As n increases by an order of magnitude, the difference
between nlogn and n2 becomes more dramatic.
n |
nlogn |
n2 |
10 |
23 |
100 |
100 |
461 |
10,000 |
1,000 |
6,908 |
1,000,000 |
10,000 |
92,103 |
100,000,000 |
100,000 |
1,151,293 |
10,000,000,000 |
1,000,000 |
13,815,511 |
1,000,000,000,000 |
If you could handle a card in a microsecond (one millionth of a second),
merge sort on n = 1,000,000 would take about 14 seconds.
For selection sort: about 278 hours
|