solutionHW#2

# solutionHW#2 - CMPSC 465 1[10 points SOLUTIONS TO...

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

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 2’s 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 the time the method finishes. That is why we had to describe what happens during both of the passes through
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