Data Str & Algorithm HW Solutions 47

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

Info iconThis 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 f 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 f 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]))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online