The selection sort algorithm for a list of integer is: :
public static void selectionSort(int[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ int min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k] < min ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] int help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
We can modify the selection sort algorithm for a list of integer: :
public static void selectionSort(int[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ int min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k] < min ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] int help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
The data type of the elements used in sorting (int) must be changed to Circle:
public static void selectionSort(Circle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Circle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k] < min ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Circle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
The expression to compare Circles by their area is: list[k].getArea < min.getArea()
public static void selectionSort(Circle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Circle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Circle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
Test program for the selection sort algorithm for an array of Circle objects:
public static void main(String[] args) { Circle[] myList = new Circle[4]; // Array of Circles objs myList[0] = new Circle("red", 2); myList[1] = new Circle("blue", 1); myList[2] = new Circle("white", 5); myList[3] = new Circle("black", 3); selectionSort(myList); for ( int i = 0; i < myList.length; i++) System.out.println(myList[i]); } |
DEMO: 04-inheritance/20-sort-geom/Demo.java
Is that all there is ??? --- Wait... things will get more interesting soon 😅
|
Let's modify the selection sort algorithm for a list of Circle objects: :
public static void selectionSort(Circle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Circle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Circle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
The data type of the elements used in sorting (Circle) must be changed to Rectangle:
public static void selectionSort(Rectangle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Rectangle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // How about this expression? { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Rectangle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
The selection sort algorithm for an array of Rectangle object is:
public static void selectionSort(Rectangle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Rectangle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // It's correct !! { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Rectangle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
Test program that sorts an array of Rectangle objects:
public static void main(String[] args) { Rectangle[] myList = new Rectangle[4]; // Array of Rectangles objs myList[0] = new Rectangle("red", 2, 1); myList[1] = new Rectangle("blue", 1, 1); myList[2] = new Rectangle("white", 5, 1); myList[3] = new Rectangle("black", 3, 2); selectionSort(myList); for ( int i = 0; i < myList.length; i++) System.out.println(myList[i]); } |
DEMO: 04-inheritance/20-sort-geom/Demo2.java
|
|
Consider the selectionSort algorithm on an array of Rectangle objects: it only require the getArea() action.
public static void selectionSort(Rectangle[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ Rectangle min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // Required functionality to sort { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] Rectangle help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
Since the GeometricObject class has the getArea() method, we can sort an array of GeometricObject object:
public static void selectionSort(GeometricObject[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ GeometricObject min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // getArea() always returns 0 ?? { Is this useful in sorting ?? min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] GeometricObject help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
Review: using a superclass variable to invoke methods in a subclass object:
Java allows you to access members in a subclass object using a superclass reference variable !
This means: we can pass an array of Rectangle objects to this selectionSort( ) method:
public static void selectionSort(GeometricObject[] list) { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ GeometricObject min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // getArea() returns area { of a Rectangle !! min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] GeometricObject help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
Test program to sort an array of Rectangle object:
public static void main(String[] args) { Rectangle[] myList = new Rectangle[4]; // Array of Rectangles objs myList[0] = new Rectangle("red", 2, 1); myList[1] = new Rectangle("blue", 1, 1); myList[2] = new Rectangle("white", 5, 1); myList[3] = new Rectangle("black", 3, 2); // ****************************************************************** // We can pass an array of Rectangle to selectionSort // because a Rectangle can fulfill requests made to a GeometricObject // ****************************************************************** selectionSort(myList); for ( int i = 0; i < myList.length; i++) System.out.println(myList[i]); } |
DEMO: 04-inheritance/20-sort-geom/Demo3.java
We can also pass an array of Circle object to this selectionSort( ) method:
public static void main(String[] args) { Circle[] myList = new Circle[4]; // Array of Circles objs myList[0] = new Circle("red", 2); myList[1] = new Circle("blue", 1); myList[2] = new Circle("white", 5); myList[3] = new Circle("black", 3); // ****************************************************************** // We can pass an array of Rectangle to selectionSort // because a Rectangle can fulfill requests made to a GeometricObject // ****************************************************************** selectionSort(myList); for ( int i = 0; i < myList.length; i++) System.out.println(myList[i]); } |
DEMO: 04-inheritance/20-sort-geom/Demo4.java
We can even sort a mix of Circle and Rectangle objects:
public static void main(String[] args) { GeometricObject[] myList = new GeometricObject[4]; // Array of GeometricObject objs myList[0] = new Circle("red", 2); myList[1] = new Rectangle("blue", 1, 1); myList[2] = new Circle("white", 5); myList[3] = new Rectangle("black", 4, 4); selectionSort(myList); for ( int i = 0; i < myList.length; i++) System.out.println(myList[i]); } |
DEMO: 04-inheritance/20-sort-geom/Demo5.java
|