Recall the selection sort algorithm for an array 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;
}
}
}
|
In order to use the selectionSort algorithm to sort an array of Circle object, we must make some changes: :
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;
}
}
}
|
(1) The data type of the elements used in selectionSort( ) (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;
}
}
}
|
(2) 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 area
{
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 😅
|
In order to use the selectionSort algorithm to sort an array of Rectangle object, we must make some changes: :
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;
}
}
}
|
(1) 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;
}
}
}
|
Interestingly, the expression to compare Circles by their area is the same: list[k].getArea < min.getArea()
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() ) // Use the same 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;
}
}
}
|
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/19-apply-polym/Demo2.java
|
|
Consider the selectionSort algorithm on an array of Rectangle objects: it requires the use of getArea():
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 has the getArea() method, we can write insertionSort( ) for 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;
}
}
}
|
However: the getArea( ) method in GeometricObject is a dummy method that always returns 0 (zero)...
Review: using a superclass variable to invoke methods in a subclass object:
Java allows you to access members in a subclass (e.g.: Rectangle) using a superclass variable (e.g.: GeomObject)
This means: we can pass (= copy) an array of Rectangle objects to this selectionSort( ) method:
public static void selectionSort(GeometricObject[] list) // list can be Rectangle[]
{
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() will return
{ // 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;
}
}
}
|
If was pass an array of Rectangle objects to selectionSort( ), the getArea( ) will area of the rectangle!
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/19-apply-polym/Demo3.java
Also: we can pass (= copy) an array of Circle objects to this selectionSort( ) method:
public static void selectionSort(GeometricObject[] list) // list can be Circle[]
{
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() will return
{ // area of a Circle !!
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;
}
}
}
|
If was pass an array of Circle objects to selectionSort( ), the getArea( ) will area of the circle!
Demo program: 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/19-apply-polym/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); // a Circle
myList[1] = new Rectangle("blue", 1, 1); // a Rectangle
myList[2] = new Circle("white", 5); // a Circle
myList[3] = new Rectangle("black", 4, 4); // a Rectangle
selectionSort(myList);
for ( int i = 0; i < myList.length; i++)
System.out.println(myList[i]);
}
|
DEMO: 04-inheritance/19-apply-polym/Demo5.java
|