Sorting

  • Goal of sorting:

    • Rearrange N items such that their keys are in ascending (increasing) order

    Example:

      Input:      "John"   "Alex"   "Peter"    "Jack"
      Output:     "Alex"   "Jack"   "John"     "Peter"
    

  • Sorting algorithms are based on the following operations:

    1. Comparing 2 items

    2. Exchange 2 items

  • The primitive operation in sorting algorithm is the compare operation

Comparing items: the Java Comparable interface

  • Sorting requires that 2 items can be compared.

  • Java defines the Comparable<T> interface to represent this requirement:

      public Interface Comparable<T>
      {
          public int compareTo(T o);
      }
    

  • Classes that implement the Comparable<T> interface are comparable classes

  • Requirements of the compareTo( ) method:

     a.compareTo(b)  returns a negative value if a is less than b
     a.compareTo(b)  returns   zero (0)       if a is equal to b
     a.compareTo(b)  returns a positive value if a is greater than b
    

  • Previously, we have implemented the Comparable<T> interface in the Circle class :

Exchanging 2 items (objects)

  • Review: exchanging the content of 2 variables:

  • Method to exchange (= swap) array elements a[i] and a[j]:

       private static void exch( DataType[] a, int i, int j )
       {
           DataType help;
    
           help = a[i];
           a[i] = a[j];
           a[j] = help;
       }
    

DEMO: demo/14-sort/01-exchange/Demo.java

Defining a generic (parameterize) exch( ) method

  • This exch( ) Method will only work on objects of the type DataType: (Demo2.java)

       private static void exch( DataType[] a, int i, int j )
       {
           DataType help;
    
           help = a[i];
           a[i] = a[j];
           a[j] = help;
       }
    

  • To define a generic (parameterized) exch( ), we use the following syntax:

       private static <T> void exch( T [] a, int i, int j )
       {
            T help;
    
           help = a[i];
           a[i] = a[j];
           a[j] = help;
       }
    

DEMO: demo/14-sort/01-exchange/Demo2.java + Demo3.java + Demo4.java

Defining a generic (parameterize) exch( ) method that only works for Comparable objects

  • Unfortunately, this exch( ) Method will work on non-Comparable objects: (Demo5.java)

       private static void exch( DataType[] a, int i, int j )
       {
           DataType help;
    
           help = a[i];
           a[i] = a[j];
           a[j] = help;
       }
    

  • We can limit the parameterized type to a subclass of Comparable<T> as follows:

       private static <T extends Comparable<T>> void exch( T [] a, int i, int j )
       {
            T help;
    
           help = a[i];
           a[i] = a[j];
           a[j] = help;
       }
    

DEMO: demo/14-sort/01-exchange/Demo5.java + Demo6.java + Demo7.java

Goal of this chapter

  • Historically, sorting is an important application of computer programming

    • I once heard a report that the world's computer spend over 50% of their time running sorting algorithms...

  • Over the years, many different sorting algorithms have been developed

  • We aim to study the running time of various sorting algorithms

  • Why study their running times:

    • It will help you choose the right algorithm to solve a sorting problem