{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

55PS3 - SOLUTION SET 3DUE Please report any errors in this...

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

View Full Document Right Arrow Icon
SOLUTION SET 3—DUE 2/13/2008 Please report any errors in this document to Ian Sammis ( [email protected] ). Problem 1 (#3.1.4) . Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it. Solution. We need to compute the n 1 differences, and keep track of the biggest so far. procedure maxDiff( n Z , a 1 ...a n Z ) begin if n < 2 then throw error m := a 2 a 1 for i := 2 to n 1 do if m < a n +1 a n then m := a n +1 a n end for return m end procedure Problem 2 (#3.1.44) . Describe an algorithm based on the binary search for de- termining the correct position in which to insert an element into an already sorted list. Solution. This is almost like a binary search, except that we’re looking for a position n to insert the new element. We’ll return the position of the first element bigger than the one we’re inserting. Let F be whatever set the list elements are drawn from. Let b be the new element that we’re trying to add. procedure insertPos( n Z , b F , a 1 ...a n F ) begin if a 1 > b return 1 if a n < b return n+1 l := 1 h := n while h l > 1 do m := ( n + 1) / 2 if a m < b then l := m else h := m end while return h end procedure Problem 3 (#3.1.48) . Compare the number of comparisons used by the insertion sort and the binary insertion sort to sort the list 7, 4, 3, 8, 1, 5, 4, 2. 1
Image of page 1

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

View Full Document Right Arrow Icon
2 SOLUTION SET 3—DUE 2/13/2008 Solution. Let’s run the insertion sort first: First step: 4 compared with 7, inserted before. 1 comparison. List: 4,7 ,3,8,1,5,4,2 Second Step: 3 compared with 4, inserted before. 1 comparison (2 total so far). List: 3,4,7 ,8,1,5,4,2 Third Step: 8 compared with 3. 8 compared with 4. 8 compared with 7, inserted after. 3 comparisons (5 total so far).List: 3,4,7,8 ,1,5,4,2 Fourth Step: 1 compared with 3, inserted before. 1 comparison (6 total so far). List: 1,3,4,7,8 ,5,4,2 Fifth Step: 5 compared with 1. 5 compared with 3. 5 compared with 4. 5 compared with 7, inserted before 4 comparisons (10 total so far). List: 1,3,4,5,7,8 ,4,2 Sixth Step: 4 compared with 1. 4 compared with 3 4 compared with 4, inserted before. 3 comparisons (13 total so far). List 1,3,4,4,5,7,8 ,2 Seventh Step: 2 compared with 1. 2 compared with 3, inserted before. 2 comparisons (15 total so far). List sorted. So, an insertion sort run takes 15 comparisons. Now let’s look at a binary search for comparison. First step: 4 compared with 7, inserted before. 1 comparison. List: 4,7 ,3,8,1,5,4,2 Second Step: 3 compared with 4, inserted before. 1 comparison (2 total so far). List: 3,4,7 ,8,1,5,4,2 Third Step: 8 compared with 4. 8 compared with 7, inserted after 2 comparisons (4 total so far). List: 3,4,7,8 ,1,5,4,2 Fourth Step: 1 compared with 4.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern