CMPS 10 Winter 2011- Homework Assignment 3 Chapter 2 (p.76 ...
CMPS 10 Winter 2011- Homework Assignment 3 Chapter 2 (p.76 ...
CMPS 10 Winter 2011- Homework Assignment 3 Chapter 2 (p.76 ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>CMPS</strong> <strong>10</strong> <strong>Winter</strong> <strong>2011</strong>- <strong>Homework</strong> <strong>Assignment</strong> 3<br />
Problems:<br />
<strong>Chapter</strong> 2 (<strong>p.76</strong>): 20, 21, 22<br />
<strong>Chapter</strong> 3 (p.120): 3abc, 6, 17abc, 18, 19, 20, 21, 22<br />
<strong>Chapter</strong> 2:<br />
20. Design an algorithm that is given a positive integer N and determines whether N is a<br />
prime number, that is, not evenly divisible by any value other than 1 and itself. The<br />
output of your algorithm is either the message “not prime”, along with a factor of N, or<br />
the message “prime”.<br />
Solution:<br />
Step Operation<br />
1.) Input N<br />
2.) set i to 2<br />
3.) while ( i
6.) code_dist = dist + k<br />
7.) if ( code_dist < 26 )<br />
8.) Codei = ‘a’ + code_dist<br />
9.) else<br />
<strong>10</strong>.) Codei = ‘a’ + code_dist – 26<br />
11.) set i to i+1<br />
12.) print Code<br />
13.) STOP<br />
Explanation: This algorithm assumes that alphabets ‘a’ –‘z’ have consecutive values<br />
assigned to them, for example, as in ASCII code. (see page 141 in Text, 5 th ed). Thus ‘b’<br />
=’a’+1, ‘c’ = ‘a’ + 2 so on. Each character has a fixed distance from ‘a’. In step 5, we<br />
compute the distance of ith letter in text from ‘a’. In step 6, we add k to this value to get<br />
distance of coded letter. If this code_dist is less then 26, we can get the character by adding it<br />
to ‘a’ as in step 8. If code_dist is greater than 26, we wrap around. A code_dist of 27 would<br />
actually be distance 1.<br />
22. Design and implement an algorithm that is given as input an integer value k 0<br />
and a list of k numbers N1, N2, … , Nk. Your algorithm should reverse the order of the<br />
numbers in the list. That is, if the original list contained:<br />
N1 = 5, N2 = 13, N3 = 8, N4 = 27, N5 = <strong>10</strong> (k = 5)<br />
Then when your algorithm has completed, the values stored in the list will be:<br />
N1 = <strong>10</strong>, N2 = 27, N3 = 8, N4 = 13, N5 = 5.<br />
Solution:<br />
There are several possible solutions to this problem. Here is one possible algorithm:<br />
Step Operation<br />
1.) Input k and N1 … Nk<br />
2.) Create and set i to 1, and create Temp<br />
3.) Repeat steps 4, 5, 6, and 7 while (i k/2) (See note in explanation)<br />
4.) Set Temp to Ni<br />
5.) Set Ni to N(k + 1 – i)<br />
6.) Set N(k + 1 – i) to Temp<br />
7.) Set i to i + 1<br />
8.) STOP<br />
Explanation: This algorithm reverses the list in place by utilizing a temporary storage<br />
variable. One could replace lines 4-6 with a line saying “Swap Ni and N(k + 1 – i)” if an<br />
appropriate “swap” algorithm were available. In step 3, we assume integer division for<br />
“k/2,” which truncates the answer. For example, if k = 5, we get 5/2 = 2. Therefore, again<br />
using k = 5, we swap N1 with N(5 + 1 – 1) (or N5) and we swap N2 with N(5 + 1 – 2) (or N4). Due<br />
to integer division, we stop at this point and leave N3 alone, which is correct. If we are using<br />
real number division, the algorithm will attempt to swap N3 with N3. This does not change
the answer or break the algorithm, but it is a wasted operation. If k is an even number, then<br />
the type of division does not matter since there is no “middle element” that would be swapped<br />
with itself.<br />
<strong>Chapter</strong> 3:<br />
3. A tennis tournament has 342 players. A single match involves 2 players. The winners<br />
of a match will play the winner of a match in the next round, whereas losers are<br />
eliminated from the tournament. The 2 players who have won all previous rounds play<br />
in the final game, and the winner wins the tournament. What is the total number of<br />
matches needed to determine the winner?<br />
a. Here is one algorithm to answer this question. Compute 342 / 2 = 171 to get the<br />
number of pairs (matches) in the first round, which results in 171 winners to go on to the<br />
second round. Compute 171 / 2 = 85 with 1 left over, which results in 85 matches in the<br />
second round and 85 winners, plus the 1 left over, to go to the third round. For the third<br />
round compute 86 / 2 = 43, so the third round has 43 matches, and so on. The total<br />
number of matches is 171 + 85 + 43 + …. Finish this process to find the total number of<br />
matches.<br />
Solution:<br />
The number of matches in the fourth round is 43 / 2 = 21, with 1 left over<br />
The number of matches in the fifth round is 22 / 2 = 11<br />
The number of matches in the sixth round is 11 / 2 = 5, with 1 left over<br />
The number of matches in the seventh round is 6 / 2 = 3<br />
The number of matches in the eighth round is 3 / 2 = 1, with 1 left over<br />
The number of matches in the fifth round is 2 / 2 = 1<br />
So, the total number of matches is<br />
171 + 85 + 43 + 21 + 11 + 5 + 3 + 1 + 1 = 341<br />
b. Here is another algorithm to solve the problem. Each match results in exactly one<br />
loser, so there must be the same number of matches as losers in the tournament.<br />
Compute total number of losers in the entire tournament. (Hint: This isn’t really a<br />
computation; it is a one-sentence argument.)<br />
Solution:<br />
At the end of the tournament, there is only one winner, 341 losers. Since each match results in<br />
exactly one loser, the total number of matches is 341 for the tournament.<br />
c. What are your opinions on the relative clarity, elegance, and efficiency of the two<br />
algorithms?<br />
Solution:<br />
Both algorithms are clear and easy to understand, but the second algorithm is more elegant<br />
and much efficient. Especially when the total number of players in the tournament is large,<br />
there is a big difference in term of efficiency.
6. Perform a selection sort on the list 7, 4, 2, 9, 6. Show the list after each exchange<br />
that has an effect on the list ordering.<br />
Solution:<br />
Initial List: 7, 4, 2, 9, 6<br />
Exchange 6 and 9: 7, 4, 2, 6, 9<br />
Exchange 7 and 6: 6, 4, 2, 7, 9<br />
Exchange 6 and 2: 2, 4, 6, 7, 9<br />
List sorted: 2, 4, 6, 7, 9<br />
17. Consider the following list of names.<br />
Arturo, Elsa, JoAnn, John, Jose, Lee, Snyder, Tracy<br />
1 2 3 4 5 6 7 8<br />
a. Use binary search to decide whether Elsa is in this list. What names will be<br />
compared to Elsa?<br />
Solution:<br />
(The names being compared are bold and italic.)<br />
There are totally 8 names in the list. So the midpoint position is location 4, which is<br />
John. So we first compare the target name Elsa with John. Elsa precedes John, so we<br />
go to the front half of the list and compare Elsa with the midpoint of this half<br />
segment at location 2, which is Elsa – the target name is found!<br />
b. Use binary search to decide whether Tracy is in this list. What names will be<br />
compared to Tracy?<br />
Solution:<br />
(The names being compared are bold and italic.)<br />
1) Compare Tracy with the midpoint of the whole list, which is John. Since Tracy<br />
follows John, go to the back half segment [5,6,7,8].<br />
2) Compare Tracy with the midpoint of the segment [5,6,7,8], which is Lee. Since<br />
Tracy follows Lee, go the back half segment [7,8].<br />
3) Compare Tracy with midpoint of the segment [7,8], which is Snyder. Since Tracy<br />
follows Snyder, go to the back half segment [8].<br />
4) Compare Tracy with Tracy at location 8 – the target name is found!<br />
c. Use binary search to decide whether Emile is in this list. What names will be<br />
compared to Emile?<br />
Solution:<br />
(The names being compared are bold and italic.)<br />
1) Compare Emile with the midpoint of the whole list, which is John. Since Emile<br />
precedes John, go to the front half segment [1,2,3].
2) Compare Emile with the midpoint of the segment [1,2,3], which is Elsa. Since<br />
Emile follows Elsa, go to the back half segment [3].<br />
3) Compare Emile with JoAnn at location 3 – not match! So Emile is not in the list.<br />
18. Use the binary search algorithm to decide whether 35 is in the following list:<br />
3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43<br />
What numbers will be compared to 35?<br />
Solution:<br />
The binary search algorithm compares the target number (35) to the middle number of<br />
each segment it searches. Thus it compares 35 to 14, then 22, then 31 (rounding down<br />
for the middle value), and then 43. Thus it compares four numbers to 35: 14, 22, 31, 43.<br />
19. If a list is already sorted in ascending order, a modified sequential search algorithm<br />
can be used that compares against each element in turn, stopping if a list element<br />
exceeds the target value. Write a pseudo code version of this short sequential search.<br />
One possible solution:<br />
1. Get value for n, the number of elements in the list<br />
2. Get value for Target, the target value is being searched<br />
3. Set the value of i to 1<br />
4. While i is less than or equal to n, do steps 5 through 13<br />
5. If Target is equal to the value of the ith element in the list<br />
6. Print the message “The target value is found: ”<br />
7. Print the value of the ith element<br />
8. Go to Step 14 (Exit from the “While” loop)<br />
9. Else if Target is greater than the value of the ith element<br />
<strong>10</strong>. Print the message “The target value is not in the list”<br />
11. Go to Step 14<br />
12. Else (Target is less than the value of the ith element)<br />
13. Increment i by 1<br />
14. Stop<br />
20. This exercise refers to short sequential search (see Exercise 19).<br />
a. What is the worst case number of comparisons of short sequential search on sorted<br />
n-element list?<br />
b. What is the approximate average number of comparisons to find an element that is in<br />
a sorted list using short sequential search?<br />
c. Is short sequential search ever more efficient than regular sequential search? Explain.<br />
Solution:<br />
a. Worst case comparisons is n.<br />
b. Average number of comparisons is (n+1)/2.
c. The algorithms have similar efficiency.<br />
21. Draw the tree structure that describes binary search on the 8-element list in Excise<br />
17. What is the number of comparisons in the worst case? Give an example of a<br />
name to search for that requires that many comparisons.<br />
Solution:<br />
The worst case scenario requires logN+1 = 4 comparisons. See Excise 17(b) for the<br />
example.<br />
22. Draw the tree structure that describes binary search on a list with 16 elements.<br />
What is the number of comparisons in the worst case?<br />
Solution:<br />
John<br />
Elsa Lee<br />
Arturo JoAnn Jose Snyder<br />
Tracy<br />
Because there are five levels to this tree, the worst case scenario occurs with element 16<br />
and requires 5 comparisons.