Data Str & Algorithm HW Solutions 47

Data Str & Algorithm HW Solutions 47 - 47 7.3 The...

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

View Full Document Right Arrow Icon
47 7.3 The revised algorithm will work correctly, and its asymptotic complexity will remain Θ( n 2 ) . However, it will do about twice as many comparisons, since it will compare adjacent elements within the portion of the list already known to be sorted. These additional comparisons are unproductive. 7.4 While binary search will fi nd the proper place to locate the next element, it will still be necessary to move the intervening elements down one position in the array. This requires the same number of operations as a sequential search. However, it does reduce the number of element/element comparisons, and may be somewhat faster by a constant factor since shifting several elements may be more ef fi cient than an equal number of swap operations. 7.5 (a) template <class Elem, class Comp> void selsort(Elem A[], int n) { // Selection Sort for (int i=0; i<n-1; i++) { // Select i’th record int lowindex = i; // Remember its index for (int j=n-1; j>i; j--) // Find least value if (Comp::lt(A[j], A[lowindex])) lowindex = j; // Put it in place if (i != lowindex) // Add check for exercise
Image of page 1
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