set14_sol

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/01/2011 for the course 198 112 at Rutgers.

Page1 / 5

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

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