{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw3-s10-soln

# hw3-s10-soln - CS4102, Spring 2010. Homework 3: ...

This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: CS4102, Spring 2010. Homework 3: Solutions Textbook, p. 253. Question 13. This depends on which partition algorithm is used. If we use Lomuto’s partition algorithm (p. 245 in the book), then the if ­statement fails for each iteration of the loop, and the value of h is never incremented before it is returned. Thus it remains equal to the index of the first item in the sub ­list being partitioned. This leads to the worst ­case situation for quicksort. The number of comparisons will be: Problem Rec2: See solution posted with Homework 2 solutions. Problem Sort LB 1: n n −1 W ( n ) = ∑ ( k − 1) = ∑ k = k =2 k =1 n ( n − 1) ∈ Θ( n 2 ) 2 € The decision tree argument says this about the worst ­case number of comparisons for a sorting algorithm that compares keys: W ( n ) ≥ Ⱥlg n!Ⱥ ≥ Ⱥn lg n − 1.443nȺ For n=5, n! is 120. Since lg 128 is 7, W(5) ≥ 7. So the lower ­bound for n=5 is 7. Problem Invers 1: A simple modification to merge accomplishes this task. Referring to the pseudo ­code to the right, the change is made in the final else. When the first ­item in the remaining part of B (the right ­ half of the list) is less ­than the first ­item in what’s remaining in A (the left ­half of the list), then that item from B is out ­of ­order with respect to all the remaining items in A. So keep a running sum where you add the number of remaining items in A at the start of that else ­clause. € Merge(A, B, C) // merging A and B into C if (A is empty) rest of C = rest of B else if (B is empty) rest of C = rest of A else if (first of A <= first of B) first of C = first of A merge (rest of A, B, rest of C) else // first of B < first of A first of C = first of B merge (A, rest of B, rest of C) Example: n is 8 and you’re merging the first 4 with the last 4 for this list: [1, 3, 6, 9, 0, 2, 7, 10] Right way the 0 will be placed into C. But it is an inversion with the 4 items: 1, 3, 6, 9. Later, after 1 from A is put into C, you need to put 2 into C. That means it’s an inversion with 3, 6 and 9. Later 7 is put into C before 9, so that’s another inversion. There are thus 4+3+1=9 inversions in that list. Problem QS Med3 1: You need at least 3 comparisons to find the median of three values. (Draw a decision tree to convince yourself of this.) You could code it with nested if’s, like the decision tree. If you have the median of three for the partition element, you can’t have the worst ­case where you have sub ­lists of size 0 and n ­1 to sort. But, if the median could be 2nd largest or 2nd smallest, which means you’d have sub ­lists of size 1 and n ­2 to sort. So the recurrence would be: W(n) = (n ­1) + 3 + W(1) + W(n ­2) = W(n ­2) + n + 2 ...
View Full 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