55PS3 - SOLUTION SET 3—DUE 2/13/2008 Please report any...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: SOLUTION SET 3—DUE 2/13/2008 Please report any errors in this document to Ian Sammis ( isammis@math.berkeley.edu ). 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 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....
View Full Document

This note was uploaded on 12/07/2011 for the course MATH MATH192 taught by Professor Michealalain during the Spring '11 term at Université Joseph Fourier Grenoble I.

Page1 / 6

55PS3 - SOLUTION SET 3—DUE 2/13/2008 Please report any...

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