- Design an algorithm to determine if two words are anagrams of
one another, that is, whether the two words have exactly the
same letters, only in a different order. For example,
late is an anagram of teal. How efficient is
your algorithm?
- For each of the following terms, say whether the term is true
of your algorithm for Problem 1 and explain briefly why or
why not.
- top-down
- zoom-in
- stable
- greedy
- We have a list of 2n numbers. We would like divide
the list into two sub-lists of n numbers each, L1
and L2, such that sum(L1)-sum(L2) is as large as possible.
Design an algorithm to produce two such sub-lists. Is
your algorithm top-down, bottom-up, or neither?
- Consider this algorithm for sorting an array:
INPUT: array initial[0..n-1] of values that can be ordered
for i ← 1 to n do
j ← i
while j > 0 and initial[j-1] > initial[j] do
tmp ← initial[j-1]
initial[j-1] ← initial[j]
initial[j] ← tmp
j ← j - 1
Write brief answers for each of the following:
- What is the basic operation of the algorithm?
- How many times is that operation executed for an array
of size n? You may assume that the numbers
in the array are distributed randomly.
- What is the efficiency class of this algorithm? Be
sure to justify your answer.
- Consider this recursive algorithm, foo(n):
if n = 0
return 0
else
return 3*foo(n-1) + 1
Write brief answers for each of the following:
- Define a recurrence relation for counting the
number of recursive calls made by this algorithm.
- Solve your recurrence relation to determine
how many calls are made for an input of size
n.