Exercise 10.3
-
An insertion sort is to be used to put the values
                       6      2      8      3      1      7      4
in ascending order.
Show the values as they would appear after each
pass of the sort.
-
What changes would have to be made to the insertSort method in
Example 2 in order to sort the values in descending order?
-
What might happen if, in Example 2, the while statement's first line
were written in the following form?
while (item < list[i-1] && i > 0)
-
Write a program that initializes an array with the names of the planets
ordered by their distances from the sun (Mercury, Venus, Earth,
Mars, Jupiter, Saturn, Uranus, Neptune, and Pluto) and prints them
in that order on one line. The program should then use an insertion
sort to arrange the names alphabetically. To trace the progress of the
sort, have it print the list after each pass.
-
The median of an ordered list of numerical values is defined in the
following way. If the number of values is odd, the median is the middle
value. If the number of values is even, the median is the average of
the two middle values. Write a program that first prompts the user
for the number of items to be processed, reads that many real values,
and then finds their median.
-
A sort is said to be stable if it always leaves values that are considered
to be equal in the same order after the sort. Is the insertion sort
stable? Justify your answer.
| |