Review:   the selection sort algorithm

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;
            }
        }
    } 

Problem:    write a selection sort algorithm to sort and array of Circle objects by their area

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;
            }
        }
    } 

Problem:    write a selection sort algorithm to sort and array of Circle objects by their area

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;
            }
        }
    } 

Problem:    write a selection sort algorithm to sort and array of Circle objects by their area

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;
            }
        }
    } 

Problem:    write a selection sort algorithm to sort and array of Circle objects by their area

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]);
    }


Output: Color = blue : radius = 1.0 Color = red : radius = 2.0 Color = black : radius = 3.0 Color = white : radius = 5.0

DEMO: 04-inheritance/20-sort-geom/Demo.java

Is that all there is ??? --- Wait... things will get more interesting soon 😅

Problem 2:    write a selection sort algorithm to sort and array of Rectangle objects by their area

 

 

 

  • What do you have to do if you want to write a selectionSort( ) method that sorts Rectangle objects by their area ???

Problem 2:    write a selection sort algorithm to sort and array of Rectangle objects by their area

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;
            }
        }
    } 

Problem 2:    write a selection sort algorithm to sort and array of Rectangle objects by their area

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;
            }
        }
    } 

Problem 2:    write a selection sort algorithm to sort and array of Rectangle objects by their area

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;
            }
        }
    } 

Problem 2:    write a selection sort algorithm to sort and array of Rectangle objects by their area

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]);
    }


Output: Color = blue : width = 1.0, height = 1.0 Color = red : width = 2.0, height = 1.0 Color = white : width = 5.0, height = 1.0 Color = black : width = 3.0, height = 2.0

DEMO: 04-inheritance/20-sort-geom/Demo2.java

Problem with our solution

  • We have redundant (= duplicate) code:

      • The statements in selectionSort( ) method for Circle and Rectangle is identical

        (The difference in data type is not a statement)

  • $64,000 question:

      • Can we avoid writing 2 identical method to sort similar objects ???

  • Answer:

      • ???

         

Problem with our solution

  • We have redundant (= duplicate) code:

      • The statements in selectionSort( ) method for Circle and Rectangle is identical

        (The difference in data type is not a statement)

  • $64,000 question:

      • Can we avoid writing 2 identical method to sort similar objects ???

  • Answer:

      • We can write a selectionSort() to sort objects of their superclass !!!

      • Requirement:   the superclass object must provide all the necessary actions used in the selectionSort() algorithm

Using polymorphism to make a method more general

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;
            }
        }
    } 

Using polymorphism to make a method more general

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;
            }
        }
    } 

Using polymorphism to make a method more general

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 !

Using polymorphism to make a method more general

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;
            }
        }
    } 

Using polymorphism to make a method more general

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]);
    }


Output: Color = blue : width = 1.0, height = 1.0 Color = red : width = 2.0, height = 1.0 Color = white : width = 5.0, height = 1.0 Color = black : width = 3.0, height = 2.0

DEMO: 04-inheritance/20-sort-geom/Demo3.java

Using polymorphism to make a method more general

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]);
    }


Output: Color = blue : radius = 1.0 Color = red : radius = 2.0 Color = black : radius = 3.0 Color = white : radius = 5.0

DEMO: 04-inheritance/20-sort-geom/Demo4.java

Using polymorphism to make a method more general

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]);
    }


Output: Color = blue : width = 1.0, height = 1.0 Color = red : radius = 2.0 Color = black : width = 4.0, height = 4.0 Color = white : radius = 5.0

DEMO: 04-inheritance/20-sort-geom/Demo5.java

Summary and postscript

  • A common technique to generalize code is write methods with a superclass type as parameter type

  • Such a method can receive objects of any subclass type as argument !

  • Requirement:

      • The superclass type must provide all the actions necessary to code the method



  • Java provides an interface mechanism that is similar to the inheritance mechanism for defining superclass type and subclass type relationship

  • Using this interface mechanism, we can make a "superclass" type that can unite the String , the Circle and the Rectangle classes !

  • The interface mechanism will be discussed in Chapter 13