hw6 - EECS 203 Homework 6 Solution 1(E 3.1.46 In Algorithm...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
EECS 203 Homework 6 Solution 1. (E) 3.1.46 In Algorithm 5, the variable j goes from 2 to n therefore the loop is executed for n 1 times. In the loop, only one comparison is performed ( a j > a i ). This is because the input list n, n 1 , ..., 1 is in descending order, a j > a i always evaluates false. Therefore the total number of comparisons is n 1. This is the best case for insertion sort in terms of the number of comparisons, but moving the elements to do the insertions requires much more effort. 2. (E) 3.3.4 Assume k > = 1. The algorithm is: r := x for i := 1 to k r := r * r end for { r = x 2 k } The number of multiplications is k . This is much more efficient than the naive algorithm, i.e., multiplying x by itself for 2 k 1 times. 3. (E) 3.3.8 (a) Evaluate 3 x 2 + x + 1 at x = 2. In the algorithm, c = 2 , a 0 = 1 , a 1 = 1 , a 2 = 3 , n = 2. The loop is executed for two times. Initially, y = a 2 = 3. Loop 1 ( i = 1): y y c + a n - i = 3 2 + 1 = 7. Loop 2 ( i = 2): y y c + a n - i = 7 2 + 1 = 15. Therefore 3 x 2 + x + 1 = 15 when x = 2. (b) The for loop is executed for n times. Each time we perform exactly one multiplication and one addition. Therefore the total number of multiplications is n , and the total number of additions is n as well. 4. (C) 3.3.12 (a) The number of comparisons does not depend on the values of a 1 through a n . Therefore 2 n 1 comparisons are used (or n 1 compar- isons if we do not count the comparisons used in the for statement). (b) Let the element to find be x . If x locates at a i (1 i n ), then the loop is executed for i times with two comparisons performed each time. Therefore the total number of comparisons is 2 i + 1 since there is one extra comparison outside the loop. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In the best case, x locates at a 1 . Only 2 × 1 + 1 = 3 comparisons are needed. In the average case, it is equally like that x is a 1 to a n . Therefore the average number of comparisons is n i =1 (2 i + 1) n = n + 2 n i =1 i n = n + n ( n + 1) n = n + 2 (c) Solution 1: If we look at the algorithm on the book (the box of Algorithm 3 in Section 3.1), the best case performance is the same as the worst case performance, namely O (log n ) (or more precisely, 2 log n + 2), since the list is essentially cut in half at each iteration, and the algorithm does not stop until the list has only one element left in it. Solution 2:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2010 for the course EECS 203 taught by Professor Yaoyunshi during the Spring '07 term at University of Michigan.

Page1 / 6

hw6 - EECS 203 Homework 6 Solution 1(E 3.1.46 In Algorithm...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online