ECE 369 Homework #6 Solutions (Sixth Edition)
15) In Selection Sort, the index of the maximum item in a list must be found. This
requires comparisons between list elements. In an n-element (unsorted) list how
such comparisons are needed in the worst case to find the maximum element.?
How many are needed in the average case?
n-1 compares are always needed --every element after the first must be considered
a potential new maximum.
16) Define the basic operation as the comparison of list elements and ignoring the
amount of work required to exchange list elements, write a recurrence relation for
amount of work done by selection sort on an n-element list.
(no comparisons are required since a 1-element list is always sorted)
(n-1 compares to find the maximum element + # of
compares to sort the list minus the las$
17) Solve the recurrence of (16).
a first order linear recurrence relation with constant coefficients. By
of section 2.4. the solution is
C(n) = 1^(n-1)(0) + \sum(i=2 to n)(1)^(n-i)(i-1) = (n-1)n/2
18) MergeSort requires comparing elements from each of two sorted lists to see what
goes next into
the combined sorted list.Given the following pairs of lists perform a merge and
count the number of
comparisons to merge the two lists into one.
a. 6,8,9 and 1,4,5
b. 1,5,8 and 2,3,4
c. 0,2,3,4,7,10 and 1,8,9
a. the merged list is 1,4,5,6,8,9
3 comparisons - 6 vs 1,6 vs 4,6 vs 5
b. the merged list is 1,2,3,4,5,8
4 comparisons - 1 vs 2, 5 vs 2, 5 vs 3, 5 vs 4