Review: the Selection Sort Algorithm

We have previously studied the Selection Sort algorithm when we discussed interfaces:

    public static void selectionSort(ComparableThing[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           ComparableThing 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].compareTo(min) < 0 ) // compare list[k] and min
               { 
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update its index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )   
	        exch( list, i, minIndex ); // Swap list[minIndex] and list[i]
        }
    }

Note:   this selectionSort( ) is not generic --- we will write it as a generic method

Defining a generic selectionSort( ) method

The syntax to define a generic (parameterized) method is:

    public static <T> void selectionSort(T[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
            T  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].compareTo(min) < 0 ) // compare list[k] and min
               { 
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update its index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )   
	        exch( list, i, minIndex ); // Swap list[minIndex] and list[i]
        }
    }

However:   this generic selectionSort( ) will accept arrays of non Comparable objects as parameter !!!

Defining a generic selectionSort( ) method for only Comparable objects

We can limit the type parameter T to subclasses of Comparable<T> as follows:

    public static <T extends Comparable<T> > void selectionSort(T[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
            T  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].compareTo(min) < 0 ) // compare list[k] and min
               { 
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update its index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )   
	        exch( list, i, minIndex ); // Swap list[minIndex] and list[i]
        }
    }

DEMO: demo/14-sort/02-sel-sort/Demo.java (Integer) + Demo2.java (String) + Demo3.java (NonComparable)

Runtime analysis of the Selection Sort

  • The loop structure of the Selection Sort algorithm (assuming array length = n):

      for (int i = 0; i < n-1; i++)
         for ( int k = i+1; k < n; k++ )
             doPrimitive();
    

    Analysis:

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

Runtime analysis of the Selection Sort

  • The loop structure of the Selection Sort algorithm (assuming array length = n):

      for (int i = 0; i < n-1; i++)
         for ( int k = i+1; k < n; k++ )
             doPrimitive();
    

    Analysis:

     The variable i takes on these values:
    
        i =    0    1    2    3     ....     n-2
    
    
    
    
    
    
    
    
    
    
    
    
    

Runtime analysis of the Selection Sort

  • The loop structure of the Selection Sort algorithm (assuming array length = n):

      for (int i = 0; i < n-1; i++)
         for ( int k = i+1; k < n; k++ )
             doPrimitive();
    

    Analysis:

     The variable k takes on these values for each i:
    
        i =    0    1    2    3     ....     n-3   n-2
             ----------------------------------- 
        k =    1    2    3    4    ....      n-2   n-1
               2    3    4    5    ....      n-1
               3    4    5    6
              ...  ...
              ...  n-1
              n-1  
    
    
    
    
    
    

Runtime analysis of the Selection Sort

  • The loop structure of the Selection Sort algorithm (assuming array length = n):

      for (int i = 0; i < n-1; i++)
         for ( int k = i+1; k < n; k++ )
             doPrimitive();
    

    Analysis:

     The variable k takes on these values for each i:
    
        i =    0    1    2    3     ....     n-2   n-1
             ----------------------------------- 
        k =    1    2    3    4    ....      n-2   n-1
               2    3    4    5    ....      n-1
               3    4    5    6
              ...  ...
              ...  n-1
              n-1  
    
     #Iterations = (n-1) + (n-2) + (n-3) + .... + 2 + 1  (Triangular sum !)
    
             
    
    

Runtime analysis of the Selection Sort

  • The loop structure of the Selection Sort algorithm (assuming array length = n):

      for (int i = 0; i < n-1; i++)
         for ( int k = i+1; k < n; k++ )
             doPrimitive();
    

    Analysis:

     The variable k takes on these values for each i:
    
        i =    0    1    2    3     ....     n-2   n-1
             ----------------------------------- 
        k =    1    2    3    4    ....      n-2   n-1
               2    3    4    5    ....      n-1
               3    4    5    6
              ...  ...
              ...  n-1
              n-1  
    
     #Iterations = (n-1) + (n-2) + (n-3) + .... + 2 + 1  (Triangular sum !)
                 = (n-1)n/2
             
     Runtime = O(n2)
    

Additional properties of sorting algorithms

There are 2 additional properties of sorting algorithms that are sometimes of interest:

  • In-place:

    • A sorting algorithm is in-place if it does not require another array to execute the algorithm

    The Selection Sort algorithm is an in-place sorting algorithm


  • Stable:

    • A sorting algorithm is stable if it preserves the relative order of equal keys in the array

    The Selection Sort algorithm is not stable:

     Input:      2     2      1
     Result:     1     2      2 // Relative order of 2 not maintained