You are on page 1of 3

THEORY OF ALGORITHMS

SOLUTIONS TO THE PROBLEMS

BAOJIAN HUA

MAY 10,2004

2.1-2 Rewrite the Insertion-Sort procedure to sort into nonincreasing instead of


nondecreasing order.
Solution.
[Key idea]: Only the comparision between A[i] and key needed to be modified.
[Pseudocode]:

Insertion-Sort(A)
for j ← 2 to length[A] do
key ← A[j]
i=j−1
while i > 0 and A[i] < key do
A[i + 1] ← A[i]
i← i−1
A[i + 1] ← key
[Analysis]: Its worst-case run-time is Θ(n2 ).

2.1-4 Consider the problem of adding two n-bit binary integers, sorted in two
n-element arrays A and B. The sum of the two integers should be sorted in binary
form in an (n + 1)-element array C. State the problem formally and write pseu-
docode for adding the two integers.
Solution.
[Key idea]: The follwoing figure explains our settings in expressing A, B and C:
A[1] . . . A[n]
B[1] . . . B[n]
C[1] . . . C[n] C[n + 1]
Let key ← 0 initially, then we have
C[1] = (A[1] + B[1] + key) mod 2
key = (A[1] + B[1] + key) div 2
Generally, the following equations hold
C[i] = (A[i] + B[i] + key) mod 2
key = (A[i] + B[i] + key) div 2
C[n + 1] = key

[Pseudocode]:
1
2 BAOJIAN HUA MAY 10,2004

Adding-A-B(A, B, C)
key ← 0
for i ← 1 to n do
sum ← A[i] + B[i] + key
C[i] ← sum mod 2
key ← sum div 2
C[n + 1] ← key
[Analysis]: Its worst-case run-time is Θ(n).

2.2-1 Express the function n3 /1000 − 100n2 − 100n + 3 in terms of Θ-notation.
Solution. The answer is Θ(n3 ).

2.2-2 Consider sorting n numbers sorted in array A by first finding the smallest
element of A and exchange it with the element in A[1]. Then find the second
smallest element of A, and exchange it with A[2]. Continue in this manner for the
first n − 1 element of A. Write pseudocode for this algorithm, which is known as
selection sort. What loop invariant does this algorithm maintain? Why does it
need to run for only for the first n − 1 elements, rather than for all n element? Give
the best-case and worst-case running times of selection sort in Θ-notation.
Solution.
[Key idea]: Let the pointer index point to the smallest element in any loop.
[Pseudocode]:

Selection-Sort(A)
for i ← 1 to n − 1 do
index ← i
for j ← i + 1 to n do
if A[j] < A[index] then
index ← j
if index 6= i then
A[i] ↔ A[index]
[Analysis]: Its best-case and worst-case run-time are both Θ(n2 ).

2.3-1 Using Figure 2.4 as a model, illustrate the operation of merge sort on the
array A =< 3, 41, 52, 26, 38, 57, 9, 49 >.
Solution. Straightforward.

2.3-3 Use mathematical induction to show that when n is an exact power of 2,
the solution of the recurrence

2 if n = 2;
T (n) =
2T (n/2) + n if n = 2k , for k > 1.

is T (n) = n lg n.
3

Proof. By mathematical induction on k.


Base step: when k = 1, we have
T (2k ) = T (21 ) = 2 = 2 lg 2
Inductive step: suppose the proposition holds for k where k > 1, we have
T (2k ) = 2k lg 2k = k · 2k
then for k + 1
T (2k+1 ) = 2T (2k+1 /2) + 2k+1 = 2T (2k ) + 2k+1 = 2k · 2k + 2k+1 = 2k+1 lg 2k+1
which is the wanted result.
Summary: the above two steps conclude. 
2.3-4 Insertion sort can be expressed as a recursive procedure as follows. In order
to sort A[1..n], we recursively sort A[1..n − 1] and then insert A[n] into the sorted
array A[1..n − 1]. Write a recurrence for the running time of this recursive version
of insertion sort.
Solution. The recurrence is

c n ≤ n0
T (n) =
T (n − 1) + O(n) n > n0
where n0 is determined by the concrete problem.

2.3-7 Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and
another integer x, determines whether or not there exist two elements in S whose
sum is exactly x.
Solution.
[Key idea]: Two steps: firstly, use some sort algorithm whose worst-case run-time
is Θ(n lg n) to sort the elements in the set S (for example the merge-sort algorithm);
secondly, for every element v in the sorted set S, use binary search algorithm to
search the element x − v in S.
[Pseudocode]:

Search(S, x)
Merge-Sort(S)
for i ← 1 to n do
Binary-Search(S, x − S[i])
[Analysis]: It’s easy to show that its worst-case run-time is Θ(n lg n).


You might also like