Programming and Problem Solving with Java

(やまだぃちぅ) #1
11.4 S o r t e d L i s t | 543

Table 11.1 File Sorting


Original File Sorted File


red black


blue blue


yellow brown


brown crimson


black green


pink orange


green pink


orange red


white rose


violet violet


crimson white


rose yellow


course, in this case minIndexwould no longer be an appropriate identifier and should be
changed to maxIndex.


11.4 Sorted List


ListWithSortdoes not provide the user with a true sorted list class. That is, the insertand
deletealgorithms do not preserve ordering by value. The insertoperation places a new item
at the end of the list, regardless of its value. After selectSorthas been executed, the list items
remain in sorted order only until the next insertion or deletion takes place. Of course, the
client could sort the list after every insertion, but this technique is inefficient. Let’s now look
at a sorted list design in which all the list operations cooperate to preserve the sorted order
of the list components.


Brainstorming the Sorted List


The design for the class Listsays nothing about the order of its items. If we want to keep the
list items in sorted order, we need to specify this criterion. Let’s go back to the CRC card de-
sign for Listand indicate that we want the list to be sorted.

Free download pdf