T5-D2: (Order of Growth of Functions) -- SOLUTION SKETCH
(a) The function 50n vs 0.5n2.
(b) The function 50n2 vs 0.5(2n).
Using Excel tables give better
insight to the rate of growth of the functions.
T5-Q1: (5 points) [Searching and Query Processing] -- SOLUTION SKETCH
Algorithm My-Query(Student-ID, Name, Major, Tel-Num, n, AnE); begin Print " Student-ID Name Major Tel-Num"; Print "=================================="; k <-- 1; while (k <= n ) do if (Major[k] = AnE) then Print Student-ID[k], Name[k], Major[k], Tel-Num[k]; endif; k <-- k + 1; endwhile Print "=================================="; end; |
Note: This is an example of a simply query processing
that take a linear time, Θ(n).
These types of operations are commonly done in Database Query.
T5-Q2: (10 points) [Quiz Question from the Past -- Hamming Distance] -- SOLUTION SKETCH
Algorithm Hamming-D(S, R, m); (* Hamming distance of S and R *) (* S[1..m], R[1..m] are arrays of size m *) begin count <-- 0; k <-- 1; while (k <= n ) do if (S[k] not equal R[k]) then count <-- count + 1; endif; k <-- k + 1; endwhile Hamming-Distance <-- count; return Hamming-Distance end; |
T5-D3: (Analysis of Binary Search) -- SOLUTION SKETCH
(a) The search tree for binary search algorithm
on a SORTED list with 11 names.
Tree has 11 round nodes for successful searches;
and 12 "fictitious square nodes" for unsuccessful searches;
Compare middle element A[mid], where mid=(first + last)/2.
(This give the "left" middle element for even-sized sub-arrays.)
Note: For n=1,000,000, the
average successful search time is no more than 20
(between 19 and 20),
and the average unsuccessful search time is also no more than 21
(between 20 and 21).
T5-D4: (Analysis of Sequential Search) -- SOLUTION SKETCH
(a) The search tree for sequential search algorithm.
"Tree" is linear chain with 11 round nodes for successful searches; and 1 "fictitious square nodes" for unsuccessful searches;
Note: For n=1,000,000, the
average successful search time is 500,000,
and the average unsuccessful search time is always 1,000,000.
T5-Q4: (10 points) (Analysis of Sequential Search)
(b) 5.5 = 55/10 (c) 10
[Similar to T5-D4]
T5-Q5: (15 points) [Time Complexity and How Fast They Grows]
(See this
page for the table.)