Java Reference
In-Depth Information
Let's look at an example. The program shown in Listing 10.8 uses a selection
sort to arrange a list of Contact objects into ascending order.
Listing 10.9 shows the Sorting class. It contains two static sorting algorithms.
The PhoneList program uses only the selectionSort method. The other method
is discussed later in this section.
The selectionSort method accepts an array of Comparable objects to sort.
Recall that Comparable is an interface that includes only one method, compareTo ,
which is designed to return an integer that is less than zero, equal to zero, or
greater than zero if the executing object is less than, equal to, or greater than the
object to which it is being compared, respectively.
Any class that implements the Comparable interface must define the compareTo
method. Therefore any such object can be compared to another object to deter-
mine their relative order.
The selectionSort method is polymorphic. Note that it doesn't refer to
Contact objects at all and yet is used to sort an array of Contact objects. The
selectionSort method is set up to sort any array of objects, as long as those
objects can be compared to determine their order. You can call selectionSort
multiple times, passing in arrays of different types of objects, as long as they are
Comparable .
Each Contact object represents a person with a last name, a first
name, and a phone number. Listing 10.10 shows the Contact class.
The Contact class implements the Comparable interface and
therefore provides a definition of the compareTo method. In this
case, the contacts are sorted by last name; if two contacts have the
same last name, their first names are used.
The implementation of the selectionSort method uses two for loops to sort
the array. The outer loop controls the position in the array where the next small-
est value will be stored. The inner loop finds the smallest value in the rest of the
list by scanning all positions greater than or equal to the index specified by the
outer loop. When the smallest value is determined, it is exchanged with the value
stored at the index. This exchange is done in three assignment statements by using
an extra variable called temp . This type of exchange is often called
KEY CONCEPT
Implementing a sort algorithm
polymorphically allows it to sort any
comparable set of objects.
swapping.
Note that because this algorithm finds the smallest value during each iteration,
the result is an array sorted in ascending order (that is, smallest to largest). The
algorithm can easily be changed to put values in descending order by finding the
largest value each time.
Also note that we've set up the sorting methods to sort arrays of objects.
Therefore, if your goal is to sort an array of a primitive type, such as an array of
integer values, they would have to be put into an array of Integer objects to be
processed. All of the wrapper classes implement the Comparable interface.
VideoNote
Sorting Comparable
objects.
Search WWH ::




Custom Search