Chapter_03

# Chapter_03 - Chapter 3 1. Describe an algorithm that takes...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 3 1. Describe an algorithm that takes a list of n integers a 1 , a 2 , , a n and finds the number of integers each greater than five in the list. Ans: procedure greaterthanfive ( a 1 , , a n : integers) answer : = 0 for i : = 1 to n if a i > 5 then answer : = answer + 1. 2. Describe an algorithm that takes a list of integers a 1 , a 2 , , a n ( n 2) and finds the second- largest integer in the sequence. Ans: procedure secondlargest ( a 1 , , a n : integers) largest : = a 1 secondlargest : = a 2 if a 2 > a 1 then begin secondlargest : = a 1 largest : = a 2 end if n = 2 then stop for i : = 3 to n if a i > largest then begin secondlargest : = largest largest : = a i end if ( a i > secondlargest and a i largest ) then secondlargest : = a i . 3. Describe an algorithm that takes a list of n integers ( n 1) and finds the location of the last even integer in the list, or returns 0 if there are no even integers in the list. Ans: procedure lasteven ( a 1 , , a n : integers) location : = 0 for i : = 1 to n if 2 | a i then location : = i . Page 40

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

View Full Document
4. Describe an algorithm that takes a list of n integers ( n 1) and finds the average of the largest and smallest integers in the list. Ans: procedure avgmaxmin ( a 1 , , a n : integers) max : = a 1 min : = a 1 for i : = 2 to n begin if a i > max then max : = a i if a i < min then min : = a i end avg : = ( max + min ) / 2. 5. Describe in words how the binary search works. Ans: To search for x in an ordered list a 1 , , a n , find the “midpoint” of the list and choose the appropriate half of the list. Continue until the list consists of one element. Either this element is x , or else x is not in the list. 6. Show how the binary search algorithm searches for 27 in the following list: 5 6 8 12 15 21 25 31. Ans: The consecutive choices of sublists of the original list are: 15 21 25 31, 25 31, and 25. Since 27 25, the integer 25 is not in the list. 7. You have supplies of boards that are one foot, five feet, seven feet, and twelve feet long. You need to lay pieces end-to-end to make a molding 15 feet long and wish to do this using the fewest number of pieces possible. Explain why the greedy algorithm of taking boards of the longest length at each stage (so long as the total length of the boards selected does not exceed 15 feet) does not give the fewest number of boards possible. Ans: The greedy algorithm first chooses a 12-foot-long board, and then three one-foot- long boards. This requires four boards. But only three boards are needed: each five feet long. 8. Use the definition of big-oh to prove that 1 2 + 2 2 + ... + n 2 is O ( n 3 ). Ans: 1 2 + 2 2 + ... + n 2 n 2 + n 2 + ... + n 2 = n n 2 = n 3 . 9. Use the definition of big-oh to prove that 3 384 21 n n −− n is O ( n 2 ). Ans: 3333 3 2 4 3 8 4 1 5 15 2 nn n n n n n + + ≤= 38 if n 1. = 10. Use the definition of big-oh to prove that 1 3 + 2 3 + ... + n 3 is O ( n 4 ). Ans: 1 3 + 2 3 + ... + n 3 n 3 + n 3 + ... + n 3 = n n 3 = n 4 . Page 41
11. Use the definition of big-oh to prove that 5 2 64 73 nn n 4 + is O ( n 3 ).

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/19/2009 for the course BUSINESS 2400 taught by Professor Deutch-salamon during the Fall '08 term at York University.

### Page1 / 15

Chapter_03 - Chapter 3 1. Describe an algorithm that takes...

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

View Full Document
Ask a homework question - tutors are online