{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter_03

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

This preview shows pages 1–3. 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 3 8 4 2 1 n n n is O ( n 2 ). Ans: 3 3 3 3 3 2 4 3 8 4 15 15 2 1 2 n n n n n n n n n n n + + = 3 8 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 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern