solutionHW#4 - CSE 465 SOLUTIONS TO ASSIGNMENT 4 This...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 465 SOLUTIONS TO ASSIGNMENT 4 Spring 2008 This assignment is worth a total of 50 points. 1. [no points] Exercises 5.1.5 and 5.1.7 on page 163. Exercise 5.1.5 In the pseudocode for insertion sort on page 161, the test condition in the while -loop is j 0 and A [ j ] > v . To eliminate the in-bound condition j 0, we must be sure that the second part of the test condition stops the loop when we go past the beginning of the data, so the sentinel value must fail the test of being greater than v , i.e,, it must be less than or equal to v , which means that the sentinel must be at least as small as all of the data. (In pseudocode, one would simply write -∞ for this value. For the way to achieve this in C++, see the answer to Problem 4 on the last assignment. In C++, one would also need to store the data starting at index 1, so that index 0 could be used for the sentinel.) Shortening the test this way just speeds up the code by a very small percentage; it does not change the efficiency class. Exercise 5.1.7 The version of insertion sort that uses swaps makes the same number of key comparisons, and its running time will be in the same efficiency class in terms of Θ-notation. However, it will be a bit slower because, even if the swap procedure is coded in-line, swap will still use two data transfers each time. (The original code only used one each time, plus one at the beginning and one at the end. It also used fewer array accesses.)
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern