1. (30 marks)
Suppose that you have to choose between three algorithms A1, A2 and As for a given problem. The worst-case
running time for each algorithm is given by
W2 (n) = 100n;
Ws(n) = 0.1n2.
Answer the following questions, explaining your assumptions and possible limitations of your analysis.
(a) Order the algorithms from fastest to slowest in the sense of asymptotic worst-case running time.
(b) Suppose that we run each algorithm on a large problem instance of size n. Then we feed in an input of
size 100n and re-run the algorithrss on the new input. What would you expect to happen to the running
times, and why?
(c) Suppose that hardware limitations mean that your algorithm can only accept input of size at most 106
Which algorithm would you prefer, and why? Answer the same question with 106 replaced by 10³.
(d) For which problem sizes is A1 the best algorithrn to use? Answer the same question for A2 and then for
(e) Suppose that we have 10 time units available. What is the maximum input size that can be processed by
each of the algorithms in that time?
2. (20 marks)
Formally show that 0.1n is not O( Vn) using the definition of O only.
3. (25 marks)
Consider the obvious algorithm for checking whether a list of integers is sorted: start at the beginning of the
list, and scan along until we first find a successive pair of elements that is out of order. In that case, return
false. If no such pair is found by the time we reach the end of the list, return true.
Our elementary operation is a comparison between two integers.
(a) What is the worst case running time of this algorithm on an input list containing n elements? Write all
5-element input lists which contain the numbers 1. ,2,3,4, 5 and yield the worst case.
(b) What is the best case running time of this algorithm on an input list with T2 elements? How many input
lists are there which have size n, all elements being different, and yield the best case?
(c) Suppose that the input list is a random permutation of 1.2, n and all such permutations are equally
likely. Derive the average-case expected running time. Give both an exact and asymptotic answer.
4. (25 marks)
A certain algorithm has running time T(n) on input of size 72 given by
(a) Prove that T(n) is
(b) Prove that T(n) - is O(n).
(c) Explain why the limit rule could not be used for (b).
Hint: Let L(n) == Ig - gn] and think about the properties of the function L. For example, how large can
it be, and what values does it take when T2 = 1,2,
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
In the worst case, the two elements that are out-of-order occur at the last comparison (between the last and the (n-1)th item of the list).
In this case are performed n-1 successive comparisons before the algorithm to return either TRUE (if the list is already sorted) or FALSE (if the last two elements are out-of-order).
For the provided list, it is clear that last element must be < the (n-1)th element and the rest of 3 elements must be in order.
The arrangements that yield the worst-case are (in each case are performed 5-1=4 comparisons):
1,2,3,5,4 (5,4 is out of order)
1,2,4,5,3 (5,3 is out of order)
1,3,4,5,2 (5,4 is out of order)
2,3,4,5,1 (5,1 is out of order)
If we try to move the item 5 to the left, then the out of order pair will be discovered after 3 comparisons only and the worst case is not yielded anymore....