{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

set14_sol

# set14_sol - CS112 Spring 2011 Problem Set 14 Sorting(This...

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

CS112 Spring 2011: Problem Set 14 Sorting (This problem set is being posted after recitations are over, but solutions will be posted.) ------------------------------------------------------------------------ 1. Trace the mergesort algorithm on the following linked list: 3 -> 26 -> 67 -> 25 -> 9 -> -6 -> 43 -> 82 -> 10 -> 54 Show the resulting recursion tree, with the to-be-sorted original and sub-lists at each node, and the number comparions for each merge. *SOLUTION* The original and sorted result indicated above and below: 3, 26, 67, 25, 9, -6, 43, 82, 10, 54 -6, 3, 9, 10, 25, 24, 43, 54, 67, 84 #c9 / \ / \ 3, 26, 67, 25, 9 -6, 43, 82, 10, 54 3, 9, 25, 26, 67 #c3 -6, 10, 43, 54, 82 #c4 / \ / \ / \ / \ 3, 26, 67 25, 9 -6,43,82 10,54 3, 26, 67 #c1 9,25 #c1 -6,43,82 #c2 10,54 #c1 / \ / \ / \ / \ #c1 3,26 67 9 25 #c1 -6,43 82 10 54 / \ / \ 3 26 -6 43 ------------------------------------------------------------------------ 2. What input arrangement of the set of numbers 1, 2, 3, 4, and 5 would result in the maximum number of comparisons for mergesorting them? Show the resulting mergesort recursion tree, and the number of comparisons for each merge. *Solution* One worst case input is: 1 4 2 3 5 The mergesort tree for this input is: 1 4 2 3 5 #c=4 / \ / \ #c=2 1 4 2 3 5 #c=1 / \ / \ / \ / \ #c=1 1 4 2 3 5 / \ 1 4 The number of comparisons required to merge various sorted lists are

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

View Full Document
shown against the relevant nodes (although the nodes themselves show the to-be-sorted lists before each split.) The total number of comparisons is 8. This is the maximum possible number of comparisons to sort the given input values.
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