Professional Documents
Culture Documents
Homework # 1
Q1) Suppose we are comparing implementations of insertion sort and merge sort on the same
machine. For inputs of size n, insertion sort runs in 4n2 steps, while merge sort runs in 32 n lg n
steps. For which values of n does insertion sort beat merge sort? [5 Marks]
Ans: N ≥ 50,
Q2) What is the smallest value of n such that an algorithm whose running time is 100n2 runs
faster than an algorithm whose running time is 2n on the same machine? [5 Marks]
Q3) Prove that function n3/8000 - 800n2 - 400n + 36 is Θ(n3) by finding constants n0, k1 and k2
such that following inequalities hold: [5 Marks]
Ans:
Simplify it by dividing it by n3
Lets find k1
Notice in above inequality the terms being subtracted (800/n + 400/n2) from 1/8000 are greater
than the term being added (36/n3) so the result will be a value less than 1/8000 so this inequality
will hold for k1 = 1/8000 and n0 = 1
As n gets large (800/n + 400/n2) becomes even greater than 36/n3 so this inequality will be true
for all values of n greater or equal to 1.
Lets find k2
k2 = 1/80000
n0 = 64000000
Q4) Consider sorting n numbers stored in array A by first finding the smallest element of A and
exchanging 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 elements of A. Write pseudo-
code for this algorithm, which is known as selection sort. Why does it need to run for only the
first n - 1 elements, rather than for all n elements? Give the best-case and worst-case running
times of selection sort in Θ-notation. [5 Marks]
Ans: It needs to run for only first n-1 elements because nth element will be the largest element
left in array and it will be already in its right position (last position).
Best case and worst case time will be Θ (n2) since even if array is sorted we need to traverse
entire array in order to kth find smallest element.
Q5) a) Suppose you are given p sorted arrays, each with m elements, and you want to combine
them into a single array of pm elements. Consider the following approach. Using the merge
subroutine, you merge the first 2 arrays, then merge the 3rd given array with this merged version
of the first two arrays, then merge the 4th given array with the merged version of the first three
arrays, and so on until you merge in the final (pth) input array.
What is the running time taken by this successive merging algorithm, as a function of p and m?
[3 Marks]
Ans: First merge = m + m = 2m steps
Second merge = 2m + m = 3m steps
Third merge = 3m + m = 4m
Last merge = (p-1)m + m = pm steps
Arithmetic series = 2m + 3m + 4m + …… + pm
= m ( 2 + 3+ 4 + … p) = m ( p(p+1)/2 - 1) = Theta (mp2)
Since every step is taking p time and there will be pm steps in total (there are total pm elements
and in every step one element is written to output array) so total time will be p * pm =
Theta (mp2). This can be made faster by using minHeap to find minimum element from the p
elements in each step. It will take Theta (mplgp ) time using MinHeap.
After every step one element will be extracted and one element will be inserted in minHeap
which take lg p time.
Q 6) Consider the following pseudocode for calculating ab (where a and b are positive integers)
FastPower(a,b) :
if b = 1
return a
otherwise
c := a*a
ans := FastPower(c,[b/2])
if b is odd
return a*ans
end
Here [x] denotes the floor function, that is, the largest integer less than or equal to x.
Now assuming that you use a calculator that supports multiplication and division (i.e., you can
do multiplications and divisions in constant time), what would be the overall asymptotic running
time of the above algorithm (as a function of b)?
Ans: O(log2 b). The running time only depends on b. b is divided by 2 in each recursive call so
there will be log2 b levels of recursion tree. There are constant number of operations in each
recursive call.
Q 7) Given a set N of integers and an integer y, determine if there exit two elements in N whose
absolute difference is equal to y and also print those numbers. The algorithm should take O(n lg
n) time. Justify why your algorithm runs in O(n lg n) time.
e.g. Let N = 3 , 7, 2, 1, 4, 10
y=1
Pair 1 = |3 - 2| = |-1| = 1
Pair 3 = |2 -1| = 1
Ans:
Another algorithm:
Steps 1 and 3 require O(n lg n) steps. Steps 2, 4, 5, and 6 require O(n) steps. Thus
the overall running time is O(n lg n).