Professional Documents
Culture Documents
Homework 1: Problem 1
Homework 1: Problem 1
Problem 1.
(a) (10 points) Describe an algorithm that sorts an input array A[1 · · · n] by calling a
subroutine SQRTSORT(k), which sorts the subarray A[k + 1 · · · k +√n] in place,
given an arbitrary integer k between 0 and n −√n as input. (To simplify the problem,
assume that √n is an integer.) Your algorithm is only allowed to inspect or modify
the input array by calling SQRTSORT; in particular, your algorithm must not directly
compare, move or copy array elements. How many times does your algorithm call
SQRTSORT in the worst case? Give pseudocode. Remember that you cannot use
anything other than calling the SQRTSORT routine and maybe some loops. No
formal proof of correctness is mandatory. But write a few sentences justifying your
approach.
Ans:
We will use a nested loop to sort the array. At each complete iteration of the outer loop, we
will collect n/2 greatest terms from the sub-array at the end of the array.
At each iteration of the inner loop, we will pass n terms to the SQRTSORT(n) algorithm
with n/2 terms overlapping with the last iteration. {Reason for taking n/2 terms in
explained in b) part }.
By doing so, we will accumulate n/2 greatest terms at the end of the array at each
iteration of the outer loop.
SQRTSORT(k)
*/ defined /*
helper(A, n)
if(n== 0)return
t1 = 0
t2 = n
while( t2 >= sqrt(n)/2 )
for(t1 = 0 ; t1 <= n ; )
SQRTSORT(t1)
t1 = t1 + sqrt(n)/2
end of for loop
t2 = t2 - sqrt(n)/2
end of while loop
end of helper function
Homework 1 1
How many times does my algorithm call SQRTSORT in the worst case?
2 n (2 n −1)
Total Number of calls = (2 n − 1) + (2 n − 2) + ... + 2 + 1 = 2
= (2n − n) = O(n)
In the worst-case Total number of calls to the SQRTSORT function is O(n)
(b) (10 points) Prove that your algorithm from part (a) is optimal up to constant
factors. In other words, if f(n) is the number of times your algorithm calls
SQRTSORT, prove that no algorithm can sort using o(f(n)) calls to SQRTSORT. Note
that here we are assuming that these algorithms cannot do anything other than
calling SQRTSORT repeatedly.
(Hint: Think of a typical worst case example for any sorting algorithm)
Ans:
To show that no algorithm can sort using o(f(n)) calls to SQRTSORT, we need to prove that
the number of calls to SQRTSORT(k) is minimum in our algorithm.
nk − xk = n
k= n
n −x
and m∗x=n
m = nx
Thus total Iteration = k*m
n n
= x ∗ n −x
Homework 1 2
d n n
(
dx x
∗ n −x
) =0
n −2x)
−n2 ( x2 ( n −x)2
) =0
−n2 ( n − 2x) = 0
n
x= 2
2
(3x3 −6 n x2 +4nx−n n )
d
dx
(−n2 ( x2 ( n −2x) ))
n −x)2 x=
n = ( −2n x3 (−x+ n )4
)x= n >0
2 2
This implies that the number of time our algorithm call SQRTSORT is minimum when x
= n/2
Hence no algorithm can sort using o(f(n)) calls to SQRTSORT.
(c) (10 points) Now suppose SQRTSORT is implemented recursively, by calling your
sorting algorithm from part (a). For example, at the second level of recursion, the
algorithm is sorting arrays roughly of size n1/4. What is the worst-case running time
of the resulting sorting algorithm? (To simplify the analysis, assume that the array
size n has the form 2^2^k), so that repeated square roots are always integers.)
Ans:
According to the question our helper function is calling SQRTSORT() function (2n − n)
{calculated in part (a) } which in return is calling back the helper function with the new size
as n
T (n) = n ∗ T ( n) + c ∗ n
We will solve this recurrence relation with the help of the substitution method.
T (n) = n ∗ T ( n) + c ∗ n
1
T (n) = n ∗ ( n ∗ T (n 4 ) + c ∗ n) + c ∗ n
1 1 1 1
T (n) = n ∗ (n 2 ∗ (n 22 ∗ T (n 23 ) + c ∗ n 22 ) + c ∗ n) + c ∗ n
1 1 1 1 1 1
T (n) = n ∗ (n 2 ∗ (n 22 ∗ (n 23 ∗ (n 24 ∗ ... ∗ (c)...) + c ∗ n 23 ) + c ∗ n 22 ) + c ∗ n) +
Homework 1 3
1 1 1 1 1 1 1 1 1 1 1 1
T (n) = n1+ 2 + 23 + 23 +...+ 2k + c ∗ n1+ 2 + 23 + 23 +...+ 2k−1 + c ∗ n1+ 2 + 23 + 23 +...+ 2k−2 +
Ans:
Idea - If all the combinations of the string were present, then the total number of the 0’s
and 1’s at all the j th bit of the array would be equal.
Explanation
In our algorithm, we first count the number of 0’s and 1’s in the j th bit of the string and
store them in the respective array
j = log2 (n + 1)
If the number of the 0’s at the j th bit is less than the 1’s at the j th bit then the
missing string has 0 at the j th bit
Then recursively call the same function on the array of smaller size and after returning
print the missing bit
Base case
if n = 0 then the array is empty directly return from the function
Pseudo Code -
find_string(A, n)
if(n == 0)
Homework 1 4
then return
if(n == 1)
then if(FETCHBIT(0, 0) == 0)
print "1"
return
else
print "0"
return
j = lg(n+1)/lg(2)
zero_bit= {}
one_bit = {}
for i = 0 to n
if(FETCHBIT(i, j) == 1)
then one_bit.insert(A[i])
else
zero_bit.insert(A[i])
else
find_string(one_bit, one_bit.size())
where n/2k = 1
k = log2 n
Ans:
Homework 1 5
Work done at level 0 = n
Work done at level 1 = n
15
+ n
10
+ n
6
+ n
6
1 1 2
= n[ 15 + 10 + 3]
=a n
1 1 2
where a = 15 + 10 + 3
n 1 1 2 n 1 1 2
Work done at level 2 = 15 [ 15 + 10 + 3] + 10 [ 15 + 10 + 3]
n 1 1 2 n 1 1 2
+ 6[ 15 + 10 + 3] + 6[ 15 + 10 + 3]
1 1 2 2
= n[ 15 + 10 + 3]
= a2 n
1 1 2
where a = 15 + 10 + 3
1 1 2 k
Work done at level k = n[ 15 + 10 + 3]
= ak n
Homework 1 6
k = log6 n
Total number of levels in the recursion tree k = log6 n
c na(log6 n) = c ∗ n ∗ n(log6 a)
using base change formula
1 1 2
c∗ n ∗ n(log6 a) = c ∗ n0.5log6 ( 15 + 10 + 3 )
= c ∗ n0.5+log6 1.3909
= c ∗ n0.5+0.184 = c ∗ n0.684
ans:
T(n) = O(n0.684 )
Homework 1 7