We can use Java's Comparable interface to write the selection sort algorithm to sort Comparable objects:
public static void selectionSort(Comparable[] list)
{
for (int i = 0; i < list.length - 1; i++)
{
int minIndex = findSmallest(list, i );
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
Comparable help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
In order to use selectionSort( ) method with BankAccount objects, we must implement the Comparable interface:
public class BankAccount // Original BankAccount class
{
private double balance;
public BankAccount(double x)
{
balance = x;
}
public double getBalance()
{
return balance;
}
// compareTo( ) used to compare 2 BankAccount objects
public int compareTo( BankAccount other )
{
double diff = this.getBalance()
- other.getBalance();
return (int) Math.signum(diff);
}
}
|
We change the BankAccount class to implement the Comparable interface:
public class BankAccount implements Comparable<BankAccount> { private double balance; public BankAccount(double x) { balance = x; } public double getBalance() { return balance; } // The generics feature allows us to use BankAccount as data type ! public int compareTo( BankAccount other ) { double diff = this.getBalance() - other.getBalance(); // No error ! return (int) Math.signum(diff); } } |
The generic feature allows use to pass the data type BankAccount and avoid having to use downcasting !
|
The selectionSort() method will also work for Circle objects if Circle implements Comparable (next)
In order to use selectionSort( ) method with Circle objects, we must implement the Comparable interface:
public class Circle extends GeometricObject
{
private double radius;
Circle(double r)
{
radius = r;
}
public double getArea()
{
return 3.14159*radius*radius;
}
// compareTo( ) used to compare 2 Circle objects
public int compareTo( Circle other )
{
double diff = this.getArea()
- other.getArea();
return (int) Math.signum(diff);
}
}
|
We change the Circle class to implement the Comparable interface:
public class Circle extends GeometricObject implements Comparable<Circle> { private double radius; Circle(double r) { radius = r; } public double getArea() { return 3.14159*radius*radius; } // The generics feature allows us to use Circle as data type ! public int compareTo( Circle other ) { double diff = this.getArea() - other.getArea(); // No error ! return (int) Math.signum(diff); } } |
The generic feature allows use to pass the data type BankAccount and avoid having to use downcasting !
|
The selectionSort() method will also work for Circle objects if Circle implements Comparable (next)
|
(1) We create an ArrayList for Integer (a wrapper class for int):
public static void main(String[] args)
{
ArrayList<Integer> myList = new ArrayList<>();
myList.add(4);
myList.add(3);
myList.add(6);
myList.add(2);
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
Collections.sort( myList ); // Uses upcasting !
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
}
|
Note: the Java Integer wrapper class has already implemented the Comparable interface (See: )
(2) We insert a number of integers for testing:
public static void main(String[] args)
{
ArrayList<Integer> myList = new ArrayList<>();
myList.add(4);
myList.add(3);
myList.add(6);
myList.add(2);
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
Collections.sort( myList ); // Uses upcasting !
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
}
|
Note: the Java Integer wrapper class has already implemented the Comparable interface (See: )
(3) We call Collection.sort( ) to sort the ArrayList of integers:
public static void main(String[] args)
{
ArrayList<Integer> myList = new ArrayList<>();
myList.add(4);
myList.add(3);
myList.add(6);
myList.add(2);
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
Collections.sort( myList ); // Uses upcasting !
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i) + " ");
System.out.println();
}
|
Note: the Java Integer wrapper class has already implemented the Comparable interface (See: )
We can use a similar program to sort an ArrayList of Circle objects:
public static void main(String[] args)
{
ArrayList<Circle> myList = new ArrayList<>();
myList.add(new Circle("red", 4));
myList.add(new Circle("green", 3));
myList.add(new Circle("blue", 5));
myList.add(new Circle("yellow", 2));
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i).getArea() + " ");
System.out.println();
Collections.sort( myList ); // Uses upcasting !
for ( int i = 0; i < myList.size(); i++ )
System.out.print( myList.get(i).getArea() + " ");
System.out.println();
}
|
Note: This will only work if the Circle class has implemented the Comparable interface !!
|