2024 HW2
2024 HW2
Due: 2024.3.22
2. (8%) List all the steps used to search for 8 in the sequence 2, 4, 5, 6, 7, 9, 10, 11 using
3. (6%) Use the bubble sort to sort 9, 5, 6, 4, 8, 7, showing the lists obtained at each step.
4. (10%) Let P (n) be the statement that we can form n cents of postage using just 4-cent
and 11-cent stamps.
(a) (5%) Please show that P (n) is true for all n ≥ 30 using the principle of mathematical
induction. Be sure to state explicitly your inductive hypothesis in the inductive step.
(b) (5%) Please show that P (n) is true for all n ≥ 30 using strong induction. How does
the inductive hypothesis in this proof differ from that in the inductive hypothesis for
a proof using mathematical induction?
5. (10%) Use the greedy algorithm to make change using quarters, dimes, nickels and pen-
nies for
6. (6%) How many comparisons does the insertion sort use to sort the list n, n − 1, · · · , 2, 1?
7. (6%) Use strong induction to show that every positive integer n can be written as a sum of
distinct power of two, that is, as a sum of a subset of the integers 20 = 1, 21 = 2, 22 = 4,
and so on.
8. (8%) Give a big-O estimate for each of these functions. For the function g in your estimate
f (n) is O(g), use a simple function g of smallest possible order.
10. (10%) The shaker sort (or bidirectional bubble sort) successively compares pairs of
adjacent elements, exchanging them if they are out of order, and alternatively passing
through the list from the beginning to the end and then from the end to the beginning until
no exchanges are needed.
(a) (5%) Show the steps used by the shaker sort to sort the list 3, 5, 1, 4, 6, 2.
(b) (5%) Show that the shaker sort has O(n2 ) complexity measured in terms of the
number of comparisons it uses.
11. (10%) There is a more efficient algorithm (in terms of the number of multiplications and
additions used) for evaluating polynomials than the conventional algorithm described in
the Extra Exercise 2. It is called Horner’s method. The pseudocode shows how to use
Horner method to find the value of an xn + an−1 xn−1 + · · · + a1 x + a0 at x = c.
ecprocedure Horner(c, a0 , a1 , · · ·, an : real numbers)
eccgy := an
ecfor i := 1 to n
eccgy := y∗c + an−i
ec{y = an cn + an−1 cn−1 + · · · + a1 c + a0 }
(a) (5%) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm
showing the values assigned at each assignment step.
(b) (5%) Exactly how many multiplications and additions are used by this algorithm
to evaluate a polynomial of degree n at x = c? (Do not count additions used to
increment the loop variable.)
12. (10%) The well-ordering property can be used to show that there is a unique greatest
common divisor of two positive integers. Let a and b be positive integers, and let S be the
set of positive integers of the form as + bt, where s and t are integers.
Extra Exercise:
2. The conventional algorithm for evaluating a polynomial an xn +an−1 xn−1 +· · ·+a1 x+a0
at x = c can be expressed in pseudocode by
ecprocedure polynomial(c, a0 , a1 , · · ·, an : real numbers)
eccgpower := 1
eccgy := a0
ecfor i := 1 to n
ecbegin
eccgpower := power∗c
eccgy := y + ai ∗power
ecend {y = an cn + an−1 cn−1 + · · · + a1 c + a0 }
where the final value of y is the value of the polynomial at x = c.
(a.) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm showing
the values assigned at each assignment step.
(b.) Exactly how many multiplications and additions are used to evaluate a polynomial of
degree n at x = c? (Do not count additions used to increment the loop variable.)
3. Find the least integer n such that f (x) is O(xn ) for each of these functions.