SELECTION AND MERGING 153
Exercises
1.
For N = 1000, empirically determine: the value of
k
for which the Quick-
sort-based partitioning procedure be :omes faster than using heaps to find
the
Ic
th smallest element in a
randc’m
file.
2.
Describe how you would rearrange an array of 4N elements so that the
N smallest keys fall in the first N
Isositions,
the next N keys fall in the
next N positions, the next N in the next N positions, and the N largest
in the last N positions.
3.
Show the recursive calls made when select is used to find the median of
the keys EASYQUESTION.
4. Write a program to rearrange a
file
so that all the elements with keys
equal to the median are in place, with smaller elements to the left and
larger elements to the right.
5.
What method would be best for an application that requires selection of
the
lath
largest element (for various arbitrary
Ic)
a large number of times
on the same file?
6.
True or false: the running time of mergesort does not depend on the order
of the keys in the input file. Explair. your answer.
7. What is the smallest number of steps mergesort could use (to within a
constant factor)?
8. Implement a bottom-up non-recursive mergesort that uses two arrays
instead of linked lists.
9.
Show the contents of the linked lists
l’assed
as arguments to each call when
the recursive mergesort is used to sort the keys EASY Q U E S T IO N.
10. Show the contents of the linked l&t at each iteration when the
non-
recursive mergesort is used to sort the keys EASY QUE S TIO N.
. that
we’ve
been studying).
There are two major factors which make external algorithms quite differ-
ent from those we’ve seen until now.
l’irst, the cost. the
“s:istems”
aspect of the problem is cer-
tainly as important as the algorithms
.ispect.
Both areas must be carefully
considered if an effective