This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CMPSC 465 SOLUTIONS TO ASSIGNMENT 2 Spring 2008 This assignment is worth a total of 50 points. 1. [10 points] RATIO = 7 / 5, NEXT = 7 n +1 / 5, FIRST = 5 n , so SUM = NEXT FIRST RATIO 1 = 7 n +1 / 5 5 n 2 / 5 . [8 points] This simplifies to 7 n +1 5 n +1 2 . [2 points] 2. [10 points 4+3+3] a. Selection sort is not stable : if the initial data is (2 , 2 , 1), where the primes distinguish the first 2 from the second 2, then the body of the outer loop executes twice. The first time through the outer loop, we swap A [0] and A [2], getting (1 , 2 , 2 ). The second time through the outer loop, we swap A [1] with itself, producing no further change. Thus, the two 2s have not retained their position relative to each other. Note that, in order to show that a sorting method is not stable, we must show that two equal items can end up with their order reversed after the method has finished. It is not enough to show that their order will be reversed sometime during the method if it is possible that they might still end up getting unreversed again by...
View
Full
Document
 Spring '08
 BURCHARDCHARLES

Click to edit the document details