Chapter 7 Idea of Algorithmic Efficiency Solutions
Chapter 7 Idea of Algorithmic Efficiency Solutions
CHAPTER 7
TYPE A SOLUTIONS
Answer 11
Total steps on input size 1000 =5((1000)^2) =5×10^6
Total time taken = 5X10^6X10^-9 = 5 millisecond
Answer 12
Total steps on input size 1000 =(1000)^3 =10^9
Total time taken = 10^9X10^-9 = 1 second
Answer 13
Total steps on input size 1000 =5*(1000)*log2(1000) =15000*log2(10)
Total time taken = 15000*log2(10)*10^-9 = 1 microsecond
TYPE B SOLUTIONS
Answer 1 O(n) - Due to the while loop having upper bound input i.e. n
Answer 2 O(n^3) - Due to the three nested while loop each having n and product of
three loop i.e. n^3.
Answer 3 O(5n^2) - Due to the nesting of the doIt() and having 5n in product with n for
the while loop.
Answer 4 O(n) = n^4 - Due to the nested while loop having upper bound input i.e. n
hence it gives n^2 in product with nested function doIt() having O(n) = n^2.
Answer 5 O(n) = n^3 - Due to the while loop having upper bound input i.e. n and the
nested function doIt() having O(n) = n^2. Hence O(n) =n*n^2
Answer 6 O(n) = n - As the while loop will execute for the n times because i is initialized
with n in the starting.
Answer 7 O(n) = n*p*m - The all three nested loop will be get product with upper bound
to provide the complexity.
Answer 8 O(n) = log n - As of the mid positioning property similar to binary search.
Answer 9 O(n) = 2n - The two concatenate for loop of each upper bound n will be added
to result the complexity. O(n) = n+n
Answer 10 O(n) = n^2 Since both the loops will be treated as nested loops.
Answer 11 O(n) = n - The while loop will execute for the n times as if bound has at
most limit is n.
Page 1 of 2
Answer 12 O(n) = log n - As of the mid positioning property similar to binary search.
Answer 13 O(n) = 2n - The two concatenate for loop of each upper bound n will be
added to result the complexity. O(n) = n+n
Answer 14
(i) Insertion Sort - O(n^2)
(ii) Binary Search - O(logn)
(iii) Linear Search - O(n)
Answer 15 Binary sort will be a good option as of having O(logn) time complexity than
the other mentioned options.
Page 2 of 2