This preview shows page 1. Sign up to view the full content.
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<n1; i++) { // Select i’th record
int lowindex = i;
// Remember its index
for (int j=n1; j>i; j) // Find least value
if (Comp::lt(A[j], A[lowindex]))
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.
 Fall '08
 BELL,D

Click to edit the document details