Solution Manual For Data Structures and Algorithm Analysis in C 2 E 2nd Edition 201498405
Solution Manual For Data Structures and Algorithm Analysis in C 2 E 2nd Edition 201498405
-4-
The second algorithm saves a factor of iO for each random number, and thus reduces the time
bound to OO(NOlog NO) on average. The third algorithm is clearly linear.
(c, d) The running times should agree with the preceding analysis if the machine has enough
memory. If not, the third algorithm will not seem linear because of a drastic increase for
large NO.
(e) The worst-case running time of algorithms I and II cannot be bounded because there is
always a finite probability that the program will not terminate by some given time TO. The
algorithm does, however, terminate with probability 1. The worst-case running time of the
third algorithm is linear - its running time does not depend on the sequence of random
numbers.
2.8 Algorithm 1 would take about 5 days for NO = 10,000, 14.2 years for NO = 100,000 and 140
centuries for NO = 1,000,000. Algorithm 2 would take about 3 hours for NO = 100,000 and
about 2 weeks for NO = 1,000,000. Algorithm 3 would use 11⁄2 minutes for NO = 1,000,000.
These calculations assume a machine with enough memory to hold the array. Algorithm 4
solves a problem of size 1,000,000 in 3 seconds.
2.9 (a) OO(NO2).
(b) OO(NOlog NO).
2.10 (c) The algorithm is linear.
2.11 Use a variation of binary search to get an OO(log NO) solution (assuming the array is preread).
2.13 (a) Test to see if NO is an odd number (or 2) and is not divisible by 3, 5, 7, ..., √MM
NOO.
(b) OO( √N
MMOO), assuming that all divisions count for one unit of time.
(c) BO = OO(log NO).
(d) OO(2BO/ 2).
(e) If a 20-bit number can be tested in time TO, then a 40-bit number would require about TO2
time.
(f) BO is the better measure because it more accurately represents the sizeO of the input.
-5-
-6-