SlideShare a Scribd company logo
Sorting Algorithms

          rules of the game
          shellsort
          mergesort
          quicksort
          animations
         Reference:
           Algorithms in Java, Chapters 6-8


                                              1
Classic sorting algorithms

     Critical components in the world’s computational infrastructure.
      • Full scientific understanding of their properties has enabled us
        to develop them into practical system sorts.
      • Quicksort honored as one of top 10 algorithms of 20th century
        in science and engineering.

     Shellsort.
      •Warmup: easy way to break the N2 barrier.
      •Embedded systems.

     Mergesort.
      •Java sort for objects.
      •Perl, Python stable sort.

     Quicksort.
      •Java sort for primitive types.
      •C qsort, Unix, g++, Visual C++, Python.
                                                                           2
rules of the game
shellsort
mergesort
quicksort
animations



                    3
Basic terms

     Ex: student record in a University.




     Sort: rearrange sequence of objects into ascending order.




                                                                 4
Sample sort client

     Goal: Sort any type of data
     Example. List the files in the current directory, sorted by file name.



          import java.io.File;
          public class Files
          {
             public static void main(String[] args)
             {
                File directory = new File(args[0]);
                File[] files = directory.listFiles();
                Insertion.sort(files);                           % java Files .
                for (int i = 0; i < files.length; i++)           Insertion.class
                                                                 Insertion.java
                   System.out.println(files[i]);                 InsertionX.class
             }                                                   InsertionX.java
                                                                 Selection.class
          }                                                      Selection.java
                                                                 Shell.class
                                                                 Shell.java
                                                                 ShellX.class
     Next: How does sort compare file names?
                                                                 ShellX.java
                                                                 index.html

                                                                                    5
Callbacks

     Goal. Write robust sorting library method that can sort
     any type of data using the data type's natural order.

     Callbacks.
     •  Client passes array of objects to sorting routine.
     •  Sorting routine calls back object's comparison function as needed.

     Implementing callbacks.
     • Java: interfaces.
     • C: function pointers.
     • C++: functors.




                                                                             6
Callbacks
     client
      import java.io.File;
      public class SortFiles                                          object implementation
      {                                                               public class File
          public static void main(String[] args)                      implements Comparable<File>
          {                                                           {
             File directory = new File(args[0]);
                                                                         ...
             File[] files = directory.listFiles();
                                                                         public int compareTo(File b)
             Insertion.sort(files);                                      {
             for (int i = 0; i < files.length; i++)                         ...
                System.out.println(files[i]);                               return -1;
          }
                                                                            ...
      }
                                                                            return +1;
                                                                            ...
   interface                                                                return 0;
                                              built in to Java           }
    interface Comparable <Item>
                                                                      }
    {
       public int compareTo(Item);
                                                     sort implementation
    }
                                                     public static void sort(Comparable[] a)
                                                     {
                                                        int N = a.length;
                                                        for (int i = 0; i < N; i++)
            Key point: no reference to File                for (int j = i; j > 0; j--)
                                                              if (a[j].compareTo(a[j-1]))
                                                                   exch(a, j, j-1);
                                                              else break;
                                                     }
                                                                                                        7
Callbacks

     Goal. Write robust sorting library that can sort any type of data
     into sorted order using the data type's natural order.

     Callbacks.
     •  Client passes array of objects to sorting routine.
     •  Sorting routine calls back object's comparison function as needed.

     Implementing callbacks.
     • Java: interfaces.
     • C: function pointers.
     • C++: functors.

     Plus: Code reuse for all types of data
     Minus: Significant overhead in inner loop

     This course:
     • enables focus on algorithm implementation
     • use same code for experiments, real-world data                        8
Interface specification for sorting


     Comparable interface.
     Must implement method compareTo() so that v.compareTo(w)returns:
     • a negative integer if v is less than w
     • a positive integer if v is greater than w
     • zero if v is equal to w

     Consistency.
     Implementation must ensure a total order.
     • if (a < b) and (b < c), then (a < c).
     • either (a < b) or (b < a) or (a = b).



     Built-in comparable types. String, Double, Integer, Date, File.
     User-defined comparable types. Implement the Comparable interface.




                                                                          9
Implementing the Comparable interface: example 1

   Date data type (simplified version of built-in Java code)

               public class Date implements Comparable<Date>
               {
                  private int month, day, year;                only compare dates
                                                               to other dates
                   public Date(int m, int d, int y)
                   {
                      month = m;
                      day   = d;
                      year = y;
                   }

                   public int compareTo(Date    b)
                   {
                      Date a = this;
                      if (a.year < b.year )     return   -1;
                      if (a.year > b.year )     return   +1;
                      if (a.month < b.month)    return   -1;
                      if (a.month > b.month)    return   +1;
                      if (a.day   < b.day )     return   -1;
                      if (a.day   > b.day )     return   +1;
                      return 0;
                   }
               }
                                                                                    10
Implementing the Comparable interface: example 2

     Domain names
     • Subdomain: bolle.cs.princeton.edu.
     • Reverse subdomain: edu.princeton.cs.bolle.
     • Sort by reverse subdomain to group by category.               unsorted
                                                                       ee.princeton.edu
                                                                       cs.princeton.edu
     public class Domain implements Comparable<Domain>                 princeton.edu
     {                                                                 cnn.com
        private String[] fields;                                       google.com
        private int N;                                                 apple.com
        public Domain(String name)                                     www.cs.princeton.edu
        {                                                              bolle.cs.princeton.edu
            fields = name.split(".");
            N = fields.length;
        }                                                            sorted
        public int compareTo(Domain b)
        {                                                             com.apple
                                                                      com.cnn
           Domain a = this;                                           com.google
           for (int i = 0; i < Math.min(a.N, b.N); i++)               edu.princeton
           {                                                          edu.princeton.cs
              int c = a.fields[i].compareTo(b.fields[i]);             edu.princeton.cs.bolle
              if      (c < 0) return -1;                              edu.princeton.cs.www
              else if (c > 0) return +1;                              edu.princeton.ee
           }
           return a.N - b.N;
        }
     }                           details included for the bored...                              11
Sample sort clients

 File names                                        Random numbers
  import java.io.File;                              public class Experiment
  public class Files                                {
  {                                                    public static void main(String[] args)
     public static void main(String[] args)           {
     {                                                   int N = Integer.parseInt(args[0]);
        File directory = new File(args[0]);              Double[] a = new Double[N];
                                                         for (int i = 0; i < N; i++)
        File[] files = directory.listFiles()
                                                            a[i] = Math.random();
        Insertion.sort(files);                           Selection.sort(a);
        for (int i = 0; i < files.length; i++)           for (int i = 0; i < N; i++)
           System.out.println(files[i]);                    System.out.println(a[i]);
     }                                                }
                                                    }
  }
                           % java Files .                             % java Experiment 10
                           Insertion.class                            0.08614716385210452
                           Insertion.java                             0.09054270895414829
                           InsertionX.class                           0.10708746304898642
                           InsertionX.java                            0.21166190071646818
                           Selection.class                            0.363292849257276
                           Selection.java                             0.460954145685913
                           Shell.class                                0.5340026311350087
                           Shell.java                                 0.7216129793703496
                                                                      0.9003500354411443
                                                                      0.9293994908845686



     Several Java library data types implement Comparable
     You can implement Comparable for your own types
                                                                                                12
Two useful abstractions

     Helper functions. Refer to data only through two operations.


     • less.   Is v less than w ?

         private static boolean less(Comparable v, Comparable w)
         {
            return (v.compareTo(w) < 0);
         }




     • exchange.    Swap object in array at index i with the one at index j.



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


                                                                               13
Sample sort implementations

       selection sort   public class Selection
                        {
                           public static void sort(Comparable[] a)
                           {
                              int N = a.length;
                              for (int i = 0; i < N; i++)
                              {
                                 int min = i;
                                 for (int j = i+1; j < N; j++)
                                    if (less(a, j, min)) min = j;
                                 exch(a, i, min);
                              }
                           }
                           ...
                        }


       insertion sort   public class Insertion
                        {
                           public static void sort(Comparable[] a)
                           {
                              int N = a.length;
                              for (int i = 1; i < N; i++)
                               for (int j = i; j > 0; j--)
                                  if (less(a[j], a[j-1]))
                                       exch(a, j, j-1);
                                  else break;
                          }
                           ...
                        }
                                                                     14
Why use less() and exch() ?

     Switch to faster implementation for primitive types
        private static boolean less(double v, double w)
        {
           return v < w;
        }




     Instrument for experimentation and animation

        private static boolean less(double v, double
        w)
        {
           cnt++;
           return v < w;



     Translate to other languages

         ...
         for (int i = 1; i < a.length; i++)      Good code in C, C++,
                if (less(a[i], a[i-1]))          JavaScript, Ruby....
                   return false;
             return true;}
                                                                        15
Properties of elementary sorts (review)

     Selection sort                                         Insertion sort

                                  a[i]                                                    a[i]
       i min    0   1    2   3   4 5     6   7   8   9 10    i   j   0   1    2   3   4    5 6    7   8   9 10
                S   O    R   T   E X     A   M   P   L E             S   O    R   T   E    X A    M   P   L E
       0    6   S   O    R   T   E X     A   M   P   L E     1   0   O   S    R   T   E    X A    M   P   L E
       1    4   A   O    R   T   E X     S   M   P   L E     2   1   O   R    S   T   E    X A    M   P   L E
       2   10   A   E    R   T   O X     S   M   P   L E     3   3   O   R    S   T   E    X A    M   P   L E
       3    9   A   E    E   T   O X     S   M   P   L R     4   0   E   O    R   S   T    X A    M   P   L E
       4    7   A   E    E   L   O X     S   M   P   T R     5   5   E   O    R   S   T    X A    M   P   L E
       5    7   A   E    E   L   M X     S   O   P   T R     6   0   A   E    O   R   S    T X    M   P   L E
       6    8   A   E    E   L   M O     S   X   P   T R     7   2   A   E    M   O   R    S T    X   P   L E
       7   10   A   E    E   L   M O     P   X   S   T R     8   4   A   E    M   O   P    R S    T   X   L E
       8    8   A   E    E   L   M O     P   R   S   T X     9   2   A   E    L   M   O    P R    S   T   X E
       9    9   A   E    E   L   M O     P   R   S   T X    10   2   A   E    E   L   M    O P    R   S   T X
      10   10   A   E    E   L   M O     P   R   S   T X
                A   E    E   L   M O     P   R   S   T X             A   E    E   L   M   O   P   R   S   T   X



     Running time: Quadratic (~c N2)                        Running time: Quadratic (~c N2)
     Exception:         expensive exchanges                 Exception:       input nearly in order
                        (could be linear)                                    (could be linear)

     Bottom line: both are quadratic (too slow) for large randomly ordered files
                                                                                                                  16
rules of the game
shellsort
mergesort
quicksort
animations



                    17
Visual representation of insertion sort
                         left of pointer is in sorted order   right of pointer is untouched




             a[i]




                                                                  i
Reason it is slow: data movement                                                              18
Shellsort

     Idea: move elements more than one position at a time
     by h-sorting the file for a decreasing sequence of values of h


    input   S   O     R   T   E   X   A   M   P   L   E       1-sort   A   E   L   E   O   P   M   S   X   R   T
   7-sort
                                                                       A   E   L   E   O   P   M   S   X   R   T
            M   O     R   T   E   X   A   S   P   L   E
                                                                       A   E   E   L   O   P   M   S   X   R   T
            M   O     R   T   E   X   A   S   P   L   E
                                                                       A   E   E   L   O   P   M   S   X   R   T
            M   O     L   T   E   X   A   S   P   R   E
                                                                       A   E   E   L   O   P   M   S   X   R   T
            M   O     L   E   E   X   A   S   P   R   T
                                                                       A   E   E   L   M   O   P   S   X   R   T
   3-sort
                                                                       A   E   E   L   M   O   P   S   X   R   T
            E   O     L   M   E   X   A   S   P   R   T
                                                                       A   E   E   L   M   O   P   S   X   R   T
            E   E     L   M   O   X   A   S   P   R   T
                                                                       A   E   E   L   M   O   P   R   S   X   T
            E   E     L   M   O   X   A   S   P   R   T
                                                                       A   E   E   L   M   O   P   R   S   T   X
            A   E     L   E   O   X   M   S   P   R   T
                                                                       A   E   E   L   M   O   P   R   S   T   X
            A   E     L   E   O   X   M   S   P   R   T
            A   E     L   E   O   P   M   S   X   R   T
                                                              result A     E   E   L   M   O   P   R   S   T   X
            A   E     L   E   O   P   M   S   X   R   T
            A   E     L   E   O   P   M   S   X   R   T



                         a 3-sorted file is               A    E   L   E   O   P   M   S   X   R   T
                    3 interleaved sorted files            A            E           M           R
                                                               E           O           S           T
                                                                   L           P           X
                                                                                                                   19
Shellsort

     Idea: move elements more than one position at a time
     by h-sorting the file for a decreasing sequence of values of h

     Use insertion sort, modified to h-sort

                                                                          magic increment
                                                                             sequence

     big increments:                public static void sort(double[] a)
          small subfiles            {
                                        int N = a.length;
     small increments:                  int[] incs = { 1391376, 463792, 198768, 86961,
         subfiles nearly in order                      33936, 13776, 4592, 1968, 861,
                                                       336, 112, 48, 21, 7, 3, 1 };
                                        for (int k = 0; k < incs.length; k++)
     method of choice for both          {
                                            int h = incs[k];
         small subfiles
                                            for (int i = h; i < N; i++)
         subfiles nearly in order               for (int j = i; j >= h; j-= h)
                                                    if (less(a[j], a[j-h]))
     insertion sort!
                                                         exch(a, j, j-h);
                                                    else break;
                                        }
                                    }

                                                                                         20
Visual representation of shellsort
  big increment




                                          small increment




     Bottom line: substantially faster!                     21
Analysis of shellsort

     Model has not yet been discovered (!)




                        N   comparisons      N1.289       2.5 N lg N

                    5,000       93            58             106

                   10,000      209           143             230

                   20,000      467           349             495

                   40,000      1022          855            1059

                   80,000      2266          2089           2257


                                                      measured in thousands




                                                                              22
Why are we interested in shellsort?

     Example of simple idea leading to substantial performance gains

     Useful in practice
     • fast unless file size is huge
     • tiny, fixed footprint for code (used in embedded systems)
     • hardware sort prototype

     Simple algorithm, nontrivial performance, interesting questions
     • asymptotic growth rate?
     • best sequence of increments?
     • average case performance?



     Your first open problem in algorithmics (see Section 6.8):
                  Find a better increment sequence
                     mail rs@cs.princeton.edu

     Lesson: some good algorithms are still waiting discovery          23
rules of the game
shellsort
mergesort
quicksort
animations



                    24
Mergesort (von Neumann, 1945(!))


  Basic plan:                                      plan

  • Divide array into two halves.                   M     E   R   G   E   S   O   R   T   E   X   A   M   P   L   E

  • Recursively sort each half.                     E     E   G   M   O   R   R   S   T   E   X   A   M   P   L   E


  •
                                                    E     E   G   M   O   R   R   S   A   E   E   L   M   P   T   X
    Merge two halves.                               A     E   E   E   E   G   L   M   M   O   P   R   R   S   T   X

  trace
                                           a[i]
      lo hi    0   1   2   3   4   5   6    7 8   9 10 11 12 13 14 15
               M   E   R   G   E   S   O    R T   E X A M P L E
    0      1   E   M   R   G   E   S   O    R T   E X A M P L E
    2      3   E   M   G   R   E   S   O    R T   E X A M P L E
    0      3   E   G   M   R   E   S   O    R T   E X A M P L E
    4      5   E   G   M   R   E   S   O    R T   E X A M P L E
    6      7   E   G   M   R   E   S   O    R T   E X A M P L E
    4      7   E   G   M   R   E   O   R    S T   E X A M P L E
    0      7   E   E   G   M   O   R   R    S T   E X A M P L E
    8      9   E   E   G   M   O   R   R    S E   T X A M P L E
   10     11   E   E   G   M   O   R   R    S E   T A X M P L E
    8     11   E   E   G   M   O   R   R    S A   E T X M P L E
   12     13   E   E   G   M   O   R   R    S A   E T X M P L E
   14     15   E   E   G   M   O   R   R    S A   E T X M P E L
   12     15   E   E   G   M   O   R   R    S A   E T X E L M P
    8     15   E   E   G   M   O   R   R    S A   E E L M P T X
    0     15   A   E   E   E   E   G   L    M M   O P R R S T X
                                                                                                                      25
Merging

     Merging. Combine two pre-sorted lists into a sorted whole.

     How to merge efficiently? Use an auxiliary array.
                      l           i       m            j       r
              aux[]   A   G   L   O   R   H   I   M    S   T
                                              k
              a[]     A   G   H   I   L   M



          private static void merge(Comparable[] a,
                          Comparable[] aux, int l, int m, int r)
          {
   copy      for (int k = l; k < r; k++) aux[k] = a[k];
             int i = l, j = m;
             for (int k = l; k < r; k++)
                if      (i >= m)               a[k] = aux[j++];    see book for a trick
  merge         else if (j >= r)               a[k] = aux[i++];     to eliminate these
                else if (less(aux[j], aux[i])) a[k] = aux[j++];
                else                           a[k] = aux[i++];

          }


                                                                                      26
Mergesort: Java implementation of recursive sort



      public class Merge
      {
         private static void sort(Comparable[] a,
                                  Comparable[] aux, int lo, int hi)
         {
            if (hi <= lo + 1) return;
            int m = lo + (hi - lo) / 2;
            sort(a, aux, lo, m);
            sort(a, aux, m, hi);
            merge(a, aux, lo, m, hi);
         }

          public static void sort(Comparable[] a)
          {
             Comparable[] aux = new Comparable[a.length];
             sort(a, aux, 0, a.length);
          }
      }


                        lo                       m                        hi


                        10   11   12   13   14   15   16   17   18   19
                                                                               27
Mergesort analysis: Memory

     Q. How much memory does mergesort require?
     A. Too much!
     • Original input array = N.
     • Auxiliary array for merging = N.
     • Local variables: constant.
     • Function call stack: log2 N [stay tuned].
     • Total = 2N + O(log N).
                   cannot “fill the memory and sort”




     Q. How much memory do other sorting algorithms require?
     • N + O(1) for insertion sort and selection sort.
     • In-place = N + O(log N).



     Challenge for the bored. In-place merge. [Kronrud, 1969]


                                                                28
Mergesort analysis

     Def. T(N)       number of array stores to mergesort an input of size N
                 = T(N/2)         +   T(N/2)        +    N

                      left half        right half       merge


     Mergesort recurrence             T(N) = 2 T(N/2) + N
                                                                for N > 1, with T(1) = 0

     • not quite right for odd N
     • same recurrence holds for many algorithms
     • same for any input of size N
     • comparison count slightly smaller because of array ends
                                                                                lg N   log2 N
     Solution of Mergesort recurrence               T(N) ~ N lg N

     • true for all N
     • easy to prove when N is a power of 2
                                                                                                29
Mergesort recurrence: Proof 1 (by recursion tree)


                T(N) = 2 T(N/2) + N
                                                                                    (assume that N is a power of 2)
                                           for N > 1, with T(1) = 0


                                           T(N)
                                                                                            +


                        T(N/2)                             T(N/2)                           N              = N



            T(N/4)               T(N/4)           T(N/4)            T(N/4)                  2(N/2)         = N
                                                                                            ...

  lg N
                                      T(N / 2k)                                             2k(N/2k)       = N

                                                                                            ...


         T(2)    T(2)     T(2)      T(2)      T(2)    T(2)      T(2)         T(2)         N/2 (2)         = N


                                                                                                         N lg N
                                 T(N) = N lg N
                                                                                                                  30
Mergesort recurrence: Proof 2 (by telescoping)


             T(N) = 2 T(N/2) + N
                                                                (assume that N is a power of 2)
                                  for N > 1, with T(1) = 0



     Pf.      T(N) = 2 T(N/2) + N                            given

           T(N)/N = 2 T(N/2)/N + 1                           divide both sides by N

                    = T(N/2)/(N/2) + 1                       algebra

                    = T(N/4)/(N/4) + 1 + 1                   telescope (apply to first term)

                    = T(N/8)/(N/8) + 1 + 1 + 1               telescope again

                   ...

                    = T(N/N)/(N/N) + 1 + 1 +. . .+ 1         stop telescoping, T(1) = 0

                    = lg N


                          T(N) = N lg N
                                                                                              31
Mergesort recurrence: Proof 3 (by induction)


             T(N) = 2 T(N/2) + N
                                                                  (assume that N is a power of 2)
                                   for N > 1, with T(1) = 0

     Claim. If T(N) satisfies this recurrence, then T(N) = N lg N.
     Pf. [by induction on N]
     •  Base case: N = 1.
     •  Inductive hypothesis: T(N) = N lg N
     •  Goal: show that T(2N) + 2N lg (2N).

                      T(2N) =   2 T(N) + 2N                   given
                            =   2 N lg N + 2 N                inductive hypothesis
                            =   2 N (lg (2N) - 1) + 2N        algebra
                            =   2 N lg (2N)                   QED




     Ex. (for COS 340). Extend to show that T(N) ~ N lg N for general N
                                                                                                32
Bottom-up mergesort

     Basic plan:
     • Pass through file, merging to double size of sorted subarrays.
     • Do so for subarray sizes 1, 2, 4, 8, . . . , N/2, N.     proof 4 that mergesort
                                                                 uses N lgN compares
                                             a[i]
         lo hi     0   1    2   3   4   5   6 7     8   9 10 11 12 13 14 15
                   M   E    R   G   E   S   O R     T   E X A M P L E
          0    1   E   M    R   G   E   S   O R     T   E X A M P L E
          2    3   E   M    G   R   E   S   O R     T   E X A M P L E
          4    5   E   M    G   R   E   S   O R     T   E X A M P L E
          6    7   E   M    G   R   E   S   O R     T   E X A M P L E
          8    9   E   M    G   R   E   S   O R     E   T X A M P L E
         10   11   E   M    G   R   E   S   O R     E   T A X M P L E
         12   13   E   M    G   R   E   S   O R     E   T A X M P L E
         14   15   E   M    G   R   E   S   O R     E   T A X M P E L
          0    3   E   G    M   R   E   S   O R     E   T A X M P E L
          4    7   E   G    M   R   E   O   R S     E   T A X M P E L
          8   11   E   E    G   M   O   R   R S     A   E T X M P E L
         12   15   E   E    G   M   O   R   R S     A   E T X E L M P
          0    7   E   E    G   M   O   R   R S     A   E T X E L M P
          8   15   E   E    G   M   O   R   R S     A   E E L M P T X
          0   15   A   E    E   E   E   G   L M     M   O P R R S T X


                           No recursion needed!
                                                                                         33
Bottom-up Mergesort: Java implementation

             public class Merge
             {
                   private static void merge(Comparable[] a, Comparable[] aux,
                                             int l, int m, int r)
                   {
                      for (int i = l; i < m; i++) aux[i] = a[i];
                      for (int j = m; j < r; j++) aux[j] = a[m + r - j - 1];
  tricky merge        int i = l, j = r - 1;
that uses sentinel    for (int k = l; k < r; k++)
(see Program 8.2)         if (less(aux[j], aux[i])) a[k] = aux[j--];
                          else                      a[k] = aux[i++];

            }

            public static void sort(Comparable[] a)
            {
               int N = a.length;
               Comparable[] aux = new Comparable[N];
               for (int m = 1; m < N; m = m+m)
                  for (int i = 0; i < N-m; i += m+m)
                     merge(a, aux, i, i+m, Math.min(i+m+m, N));
            }
        }

                 Concise industrial-strength code if you have the space          34
Mergesort: Practical Improvements


     Use sentinel.
     • Two statements in inner loop are array-bounds checking.
     • Reverse one subarray so that largest element is sentinel (Program 8.2)

     Use insertion sort on small subarrays.
     • Mergesort has too much overhead for tiny subarrays.
     • Cutoff to insertion sort for 7 elements.

     Stop if already sorted.
     • Is biggest element in first half   smallest element in second half?
     • Helps for nearly ordered lists.

     Eliminate the copy to the auxiliary array. Save time (but not space) by
     switching the role of the input and auxiliary array in each recursive call.



     See Program 8.4 (or Java system sort)
                                                                                   35
Sorting Analysis Summary

     Running time estimates:
     • Home pc executes 108 comparisons/second.
     • Supercomputer executes 1012 comparisons/second.



                  Insertion Sort (N2)                               Mergesort (N log N)
     computer    thousand      million       billion        thousand      million     billion
         home    instant      2.8 hours    317 years         instant       1 sec      18 min
         super   instant      1 second     1.6 weeks         instant      instant    instant



     Lesson. Good algorithms are better than supercomputers.

     Good enough?

                                18 minutes might be too long for some applications



                                                                                                36
rules of the game
shellsort
mergesort
quicksort
animations



                    37
Quicksort (Hoare, 1959)

  Basic plan.
  •   Shuffle the array.
  •   Partition so that, for some i
        element a[i] is in place
        no larger element to the left of i
        no smaller element to the right of i
  •   Sort each piece recursively.                                         Sir Charles Antony Richard Hoare
                                                                                         1980 Turing Award


          input
                       Q   U   I   C   K   S   O   R   T   E   X   A   M   P   L   E

      randomize        E   R   A   T   E   S   L   P   U   I   M   Q   C   X   O   K

       partition       E   C   A   I   E   K   L   P   U   T   M   Q   R   X   O   S

  sort left part       A   C   E   E   I   K   L   P   U   T   M   Q   R   X   O   S

 sort right part       A   C   E   E   I   K   L   M   O   P   Q   R   S   T   U   X

                       A   C   E   E   I   K   L   M   O   P   Q   R   S   T   U   X

           result
                                                                                                          38
Quicksort: Java code for recursive sort




       public class Quick
       {

               public static void sort(Comparable[] a)
               {
                  StdRandom.shuffle(a);
                  sort(a, 0, a.length - 1);
               }

               private static void sort(Comparable[] a, int l, int r)
               {
                  if (r <= l) return;
                  int m = partition(a, l, r);
                  sort(a, l, m-1);
                  sort(a, m+1, r);
           }
       }




                                                                        39
Quicksort trace


                                                          a[i]
            input     l    r    i    0   1   2   3   4    5 6    7   8   9 10 11 12 13 14 15
                                     Q   U   I   C   K    S O    R   T   E X A M P L E
       randomize                     E   R   A   T   E    S L    P   U   I M Q C X O K
        partition      0   15   5    E   C   A   I   E    K L    P   U   T M Q R X O S
                       0    4   2    A   C   E   I   E    K L    P   U   T M Q R X O S
                       0    1   1    A   C   E   I   E    K L    P   U   T M Q R X O S
                       0    0        A   C   E   I   E    K L    P   U   T M Q R X O S
                       3    4   3    A   C   E   E   I    K L    P   U   T M Q R X O S
                       4    4        A   C   E   E   I    K L    P   U   T M Q R X O S
                       6   15   12   A   C   E   E   I    K L    P   O   R M Q S X U T
                       6   11   10   A   C   E   E   I    K L    P   O   M Q R S X U T
                       6    9    7   A   C   E   E   I    K L    M   O   P Q R S X U T
  no partition for     6    6        A   C   E   E   I    K L    M   O   P Q R S X U T
 subfiles of size 1    8    9    9   A   C   E   E   I    K L    M   O   P Q R S X U T
                       8    8        A   C   E   E   I    K L    M   O   P Q R S X U T
                      11   11        A   C   E   E   I    K L    M   O   P Q R S X U T
                      13   15   13   A   C   E   E   I    K L    M   O   P Q R S T U X
                      14   15   15   A   C   E   E   I    K L    M   O   P Q R S T U X
                      14   14        A   C   E   E   I    K L    M   O   P Q R S T U X
                                     A   C   E   E   I    K L    M   O   P Q R S T U X


                                                         array contents after each recursive sort
                                                                                                    40
Quicksort partitioning

     Basic plan:
     • scan from left for an item that belongs on the right
     • scan from right for item item that belongs on the left
     • exchange
     • continue until pointers cross

                                                 a[i]

              i   j      r   0   1   2   3   4   5   6   7   8   9 10 11 12 13 14 15

             -1   15   15    E   R   A   T   E   S   L   P   U   I   M   Q   C   X   O   K

    scans     1   12   15    E   R   A   T   E   S   L   P   U   I   M   Q   C   X   O   K

 exchange     1   12   15    E   C   A   T   E   S   L   P   U   I   M   Q   R   X   O   K

              3   9    15    E   C   A   T   E   S   L   P   U   I   M   Q   R   X   O   K

              3   9    15    E   C   A   I   E   S   L   P   U   T   M   Q   R   X   O   K

              5   5    15    E   C   A   I   E   S   L   P   U   T   M   Q   R   X   O   K

              5   5    15    E   C   A   I   E   K   L   P   U   T   M   Q   R   X   O   S

                             E   C   A   I   E   K   L   P   U   T   M   Q   R   X   O   S


                                         array contents before and after each exchange
                                                                                             41
Quicksort: Java code for partitioning



      private static int partition(Comparable[] a, int l, int r)
      {
         int i = l - 1;
         int j = r;                                                                                       v
         while(true)
         {                                              i                                                 j

                                                   find item on left to swap
              while (less(a[++i], a[r]))
                 if (i == r) break;

              while (less(a[r], a[--j]))           find item on right to swap
                 if (j == l) break;
                                                                                <= v               >= v   v

              if (i >= j) break;       check if pointers cross
                                                                                       i       j

              exch(a, i, j);           swap
          }

          exch(a, i, r);    swap with partitioning item
                                                                                 <= v      v   >= v
          return i;         return index of item now known to be in place
      }                                                                                    i

                                                                                                          42
Quicksort Implementation details

     Partitioning in-place. Using a spare array makes partitioning easier,
     but is not worth the cost.

     Terminating the loop. Testing whether the pointers cross is a bit
     trickier than it might seem.

     Staying in bounds. The (i == r) test is redundant, but the (j == l)
     test is not.

     Preserving randomness. Shuffling is key for performance guarantee.

     Equal keys. When duplicates are present, it is (counter-intuitively)
     best to stop on elements equal to partitioning element.




                                                                             43
Quicksort: Average-case analysis

     Theorem. The average number of comparisons CN to quicksort a
     random file of N elements is about 2N ln N.


     • The precise recurrence satisfies C     0   = C1 = 0 and for N   2:
           CN = N + 1 + ((C0 + CN-1) + . . . + (Ck-1 + CN-k) + . . . + (CN-1 + C1)) / N

                   partition                         left   right                 partitioning
                                                                                  probability
               = N + 1 + 2 (C0 . . . + Ck-1 + . . . + CN-1) / N

     • Multiply both sides by N
                   NCN = N(N + 1) + 2 (C0 . . . + Ck-1 + . . . + CN-1)


     • Subtract the same formula for N-1:
                NCN - (N - 1)CN-1 = N(N + 1) - (N - 1)N + 2 CN-1

     • Simplify:
                           NCN = (N + 1)CN-1 + 2N
                                                                                          44
Quicksort: Average Case

                           NCN = (N + 1)CN-1 + 2N

     • Divide both sides by N(N+1) to get a telescoping sum:
           CN / (N + 1) = CN-1 / N + 2 / (N + 1)

                         = CN-2 / (N - 1) + 2/N + 2/(N + 1)

                         = CN-3 / (N - 2) + 2/(N - 1) + 2/N + 2/(N + 1)

                         = 2 ( 1 + 1/2 + 1/3 + . . . + 1/N + 1/(N + 1)   )

     • Approximate the exact answer by an integral:
                 CN      2(N + 1)( 1 + 1/2 + 1/3 + . . . + 1/N   )
                                                         N

                       = 2(N + 1) HN      2(N + 1)       dx/x
                                                     1

     • Finally, the desired result:
                  CN      2(N + 1) ln N       1.39 N lg N
                                                                             45
Quicksort: Summary of performance characteristics

     Worst case. Number of comparisons is quadratic.
     •N + (N-1) + (N-2) + … + 1 N2 / 2.
     •More likely that your computer is struck by lightning.

     Average case. Number of comparisons is ~ 1.39 N lg N.
     • 39% more comparisons than mergesort.
     • but faster than mergesort in practice because of lower cost of
       other high-frequency operations.

     Random shuffle
     • probabilistic guarantee against worst case
     • basis for math model that can be validated with experiments

     Caveat emptor. Many textbook implementations go quadratic if input:
     • Is sorted.
     • Is reverse sorted.
     • Has many duplicates (even if randomized)! [stay tuned]
                                                                           46
Sorting analysis summary

     Running time estimates:
     • Home pc executes 108 comparisons/second.
     • Supercomputer executes 1012 comparisons/second.
                  Insertion Sort (N2)                       Mergesort (N log N)
      computer   thousand      million     billion    thousand     million     billion
       home      instant      2.8 hours   317 years   instant      1 sec       18 min
       super     instant      1 second    1.6 weeks   instant     instant      instant


                                                             Quicksort (N log N)
                                                      thousand     million     billion
                                                      instant     0.3 sec          6 min
                                                      instant     instant      instant


     Lesson 1. Good algorithms are better than supercomputers.
     Lesson 2. Great algorithms are better than good ones.


                                                                                           47
Quicksort: Practical improvements

     Median of sample.
     • Best choice of pivot element = median.
     • But how to compute the median?
     • Estimate true median by taking median of sample.

     Insertion sort small files.
     • Even quicksort has too much overhead for tiny files.
     • Can delay insertion sort until end.

     Optimize parameters.                 12/7 N log N comparisons

     • Median-of-3 random elements.
     • Cutoff to insertion sort for 10 elements.

     Non-recursive version.
     • Use explicit stack.                  guarantees O(log N) stack size

     • Always sort smaller half first.

     All validated with refined math models and experiments                  48
rules of the game
shellsort
mergesort
quicksort
animations



                    49
Mergesort animation                                        merge in progress
                                                                input
                            done          untouched




                      merge in progress
                                                      auxiliary array
                           output                                              50
Bottom-up mergesort animation                                     merge in progress
                                                                       input
                        this pass           last pass




                        merge in progress
                                                        auxiliary array
                             output                                                   51
Quicksort animation

                                   i




                                           first partition




                v




              second partition




                            done       j
                                                             52

More Related Content

ZIP
Elementary Sort
PPT
Java Tutorials
ODP
Synapseindia reviews.odp.
PPT
java training faridabad
PDF
OOPs & Inheritance Notes
PPTX
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Elementary Sort
Java Tutorials
Synapseindia reviews.odp.
java training faridabad
OOPs & Inheritance Notes
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...

What's hot (20)

PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
PDF
Java 7 New Features
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
PDF
Java7 New Features and Code Examples
PDF
Java 5 and 6 New Features
PDF
Java Programming - 05 access control in java
PPT
JDK1.6
PDF
Java Day-6
PPT
9 cm604.28
PDF
Java Day-7
PDF
Scala: Object-Oriented Meets Functional, by Iulian Dragos
PDF
Java Programming - 04 object oriented in java
DOCX
Java collections notes
PDF
Collections in Java Notes
PDF
Object Oriented Programming in PHP
KEY
Groovy DSLs (JavaOne Presentation)
PDF
scalaliftoff2009.pdf
PDF
JavaParser - A tool to generate, analyze and refactor Java code
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Java 7 New Features
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Java7 New Features and Code Examples
Java 5 and 6 New Features
Java Programming - 05 access control in java
JDK1.6
Java Day-6
9 cm604.28
Java Day-7
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Java Programming - 04 object oriented in java
Java collections notes
Collections in Java Notes
Object Oriented Programming in PHP
Groovy DSLs (JavaOne Presentation)
scalaliftoff2009.pdf
JavaParser - A tool to generate, analyze and refactor Java code
Ad

Viewers also liked (20)

PPTX
Sorting Technique
PPT
CHC Finance: Using the New IRS 990 Form
PDF
Binary search algorithm
PPTX
Sorting and searching arrays binary search algorithm
DOC
Insertion sort
PPTX
Stack and Queue
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPTX
Insertion sort
PPT
Queue and stacks
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
My lecture stack_queue_operation
PDF
Binary Search Algorithm
PDF
Linear search algorithm
PDF
A project report on chat application
PPT
Searching algorithm
PPTX
Sorting algorithms
PDF
Sorting algorithms
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Sorting Algorithms
PDF
Sorting Algorithms
Sorting Technique
CHC Finance: Using the New IRS 990 Form
Binary search algorithm
Sorting and searching arrays binary search algorithm
Insertion sort
Stack and Queue
Mca ii dfs u-3 linklist,stack,queue
Insertion sort
Queue and stacks
Binary Search - Design & Analysis of Algorithms
My lecture stack_queue_operation
Binary Search Algorithm
Linear search algorithm
A project report on chat application
Searching algorithm
Sorting algorithms
Sorting algorithms
Data Structures - Lecture 8 [Sorting Algorithms]
Sorting Algorithms
Sorting Algorithms
Ad

Similar to 04 sorting (20)

PDF
21 Elementary Sorts pdf sorting technique
PDF
21 elementarysorts 2
PPT
Algorithms Binary Search recursion ppt BSIT
PDF
Mobile Software Engineering Crash Course - C02 Java Primer
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
PPTX
Algorithms and Data Structures for Sorting Numerical Data
PPT
Algorithms with-java-advanced-1.0
PDF
PDF
A comparison between C# and Java
PPT
Jug java7
PDF
Problem 1 Show the comparison of runtime of linear search and binar.pdf
PPTX
Comparable/ Comparator
PDF
Recursion Lecture in Java
PPTX
sorting.pptx
PPTX
L16-L19-Searching&Sorting&string123.pptx
PDF
Atlassian Groovy Plugins
PDF
Java cheatsheet
PDF
01 analysis-of-algorithms
PDF
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
21 Elementary Sorts pdf sorting technique
21 elementarysorts 2
Algorithms Binary Search recursion ppt BSIT
Mobile Software Engineering Crash Course - C02 Java Primer
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Algorithms and Data Structures for Sorting Numerical Data
Algorithms with-java-advanced-1.0
A comparison between C# and Java
Jug java7
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Comparable/ Comparator
Recursion Lecture in Java
sorting.pptx
L16-L19-Searching&Sorting&string123.pptx
Atlassian Groovy Plugins
Java cheatsheet
01 analysis-of-algorithms
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Weekly Chronicles - August'25 Week I
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Understanding_Digital_Forensics_Presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Dropbox Q2 2025 Financial Results & Investor Presentation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools

04 sorting

  • 1. Sorting Algorithms rules of the game shellsort mergesort quicksort animations Reference: Algorithms in Java, Chapters 6-8 1
  • 2. Classic sorting algorithms Critical components in the world’s computational infrastructure. • Full scientific understanding of their properties has enabled us to develop them into practical system sorts. • Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Shellsort. •Warmup: easy way to break the N2 barrier. •Embedded systems. Mergesort. •Java sort for objects. •Perl, Python stable sort. Quicksort. •Java sort for primitive types. •C qsort, Unix, g++, Visual C++, Python. 2
  • 3. rules of the game shellsort mergesort quicksort animations 3
  • 4. Basic terms Ex: student record in a University. Sort: rearrange sequence of objects into ascending order. 4
  • 5. Sample sort client Goal: Sort any type of data Example. List the files in the current directory, sorted by file name. import java.io.File; public class Files { public static void main(String[] args) { File directory = new File(args[0]); File[] files = directory.listFiles(); Insertion.sort(files); % java Files . for (int i = 0; i < files.length; i++) Insertion.class Insertion.java System.out.println(files[i]); InsertionX.class } InsertionX.java Selection.class } Selection.java Shell.class Shell.java ShellX.class Next: How does sort compare file names? ShellX.java index.html 5
  • 6. Callbacks Goal. Write robust sorting library method that can sort any type of data using the data type's natural order. Callbacks. • Client passes array of objects to sorting routine. • Sorting routine calls back object's comparison function as needed. Implementing callbacks. • Java: interfaces. • C: function pointers. • C++: functors. 6
  • 7. Callbacks client import java.io.File; public class SortFiles object implementation { public class File public static void main(String[] args) implements Comparable<File> { { File directory = new File(args[0]); ... File[] files = directory.listFiles(); public int compareTo(File b) Insertion.sort(files); { for (int i = 0; i < files.length; i++) ... System.out.println(files[i]); return -1; } ... } return +1; ... interface return 0; built in to Java } interface Comparable <Item> } { public int compareTo(Item); sort implementation } public static void sort(Comparable[] a) { int N = a.length; for (int i = 0; i < N; i++) Key point: no reference to File for (int j = i; j > 0; j--) if (a[j].compareTo(a[j-1])) exch(a, j, j-1); else break; } 7
  • 8. Callbacks Goal. Write robust sorting library that can sort any type of data into sorted order using the data type's natural order. Callbacks. • Client passes array of objects to sorting routine. • Sorting routine calls back object's comparison function as needed. Implementing callbacks. • Java: interfaces. • C: function pointers. • C++: functors. Plus: Code reuse for all types of data Minus: Significant overhead in inner loop This course: • enables focus on algorithm implementation • use same code for experiments, real-world data 8
  • 9. Interface specification for sorting Comparable interface. Must implement method compareTo() so that v.compareTo(w)returns: • a negative integer if v is less than w • a positive integer if v is greater than w • zero if v is equal to w Consistency. Implementation must ensure a total order. • if (a < b) and (b < c), then (a < c). • either (a < b) or (b < a) or (a = b). Built-in comparable types. String, Double, Integer, Date, File. User-defined comparable types. Implement the Comparable interface. 9
  • 10. Implementing the Comparable interface: example 1 Date data type (simplified version of built-in Java code) public class Date implements Comparable<Date> { private int month, day, year; only compare dates to other dates public Date(int m, int d, int y) { month = m; day = d; year = y; } public int compareTo(Date b) { Date a = this; if (a.year < b.year ) return -1; if (a.year > b.year ) return +1; if (a.month < b.month) return -1; if (a.month > b.month) return +1; if (a.day < b.day ) return -1; if (a.day > b.day ) return +1; return 0; } } 10
  • 11. Implementing the Comparable interface: example 2 Domain names • Subdomain: bolle.cs.princeton.edu. • Reverse subdomain: edu.princeton.cs.bolle. • Sort by reverse subdomain to group by category. unsorted ee.princeton.edu cs.princeton.edu public class Domain implements Comparable<Domain> princeton.edu { cnn.com private String[] fields; google.com private int N; apple.com public Domain(String name) www.cs.princeton.edu { bolle.cs.princeton.edu fields = name.split("."); N = fields.length; } sorted public int compareTo(Domain b) { com.apple com.cnn Domain a = this; com.google for (int i = 0; i < Math.min(a.N, b.N); i++) edu.princeton { edu.princeton.cs int c = a.fields[i].compareTo(b.fields[i]); edu.princeton.cs.bolle if (c < 0) return -1; edu.princeton.cs.www else if (c > 0) return +1; edu.princeton.ee } return a.N - b.N; } } details included for the bored... 11
  • 12. Sample sort clients File names Random numbers import java.io.File; public class Experiment public class Files { { public static void main(String[] args) public static void main(String[] args) { { int N = Integer.parseInt(args[0]); File directory = new File(args[0]); Double[] a = new Double[N]; for (int i = 0; i < N; i++) File[] files = directory.listFiles() a[i] = Math.random(); Insertion.sort(files); Selection.sort(a); for (int i = 0; i < files.length; i++) for (int i = 0; i < N; i++) System.out.println(files[i]); System.out.println(a[i]); } } } } % java Files . % java Experiment 10 Insertion.class 0.08614716385210452 Insertion.java 0.09054270895414829 InsertionX.class 0.10708746304898642 InsertionX.java 0.21166190071646818 Selection.class 0.363292849257276 Selection.java 0.460954145685913 Shell.class 0.5340026311350087 Shell.java 0.7216129793703496 0.9003500354411443 0.9293994908845686 Several Java library data types implement Comparable You can implement Comparable for your own types 12
  • 13. Two useful abstractions Helper functions. Refer to data only through two operations. • less. Is v less than w ? private static boolean less(Comparable v, Comparable w) { return (v.compareTo(w) < 0); } • exchange. Swap object in array at index i with the one at index j. private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } 13
  • 14. Sample sort implementations selection sort public class Selection { public static void sort(Comparable[] a) { int N = a.length; for (int i = 0; i < N; i++) { int min = i; for (int j = i+1; j < N; j++) if (less(a, j, min)) min = j; exch(a, i, min); } } ... } insertion sort public class Insertion { public static void sort(Comparable[] a) { int N = a.length; for (int i = 1; i < N; i++) for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break; } ... } 14
  • 15. Why use less() and exch() ? Switch to faster implementation for primitive types private static boolean less(double v, double w) { return v < w; } Instrument for experimentation and animation private static boolean less(double v, double w) { cnt++; return v < w; Translate to other languages ... for (int i = 1; i < a.length; i++) Good code in C, C++, if (less(a[i], a[i-1])) JavaScript, Ruby.... return false; return true;} 15
  • 16. Properties of elementary sorts (review) Selection sort Insertion sort a[i] a[i] i min 0 1 2 3 4 5 6 7 8 9 10 i j 0 1 2 3 4 5 6 7 8 9 10 S O R T E X A M P L E S O R T E X A M P L E 0 6 S O R T E X A M P L E 1 0 O S R T E X A M P L E 1 4 A O R T E X S M P L E 2 1 O R S T E X A M P L E 2 10 A E R T O X S M P L E 3 3 O R S T E X A M P L E 3 9 A E E T O X S M P L R 4 0 E O R S T X A M P L E 4 7 A E E L O X S M P T R 5 5 E O R S T X A M P L E 5 7 A E E L M X S O P T R 6 0 A E O R S T X M P L E 6 8 A E E L M O S X P T R 7 2 A E M O R S T X P L E 7 10 A E E L M O P X S T R 8 4 A E M O P R S T X L E 8 8 A E E L M O P R S T X 9 2 A E L M O P R S T X E 9 9 A E E L M O P R S T X 10 2 A E E L M O P R S T X 10 10 A E E L M O P R S T X A E E L M O P R S T X A E E L M O P R S T X Running time: Quadratic (~c N2) Running time: Quadratic (~c N2) Exception: expensive exchanges Exception: input nearly in order (could be linear) (could be linear) Bottom line: both are quadratic (too slow) for large randomly ordered files 16
  • 17. rules of the game shellsort mergesort quicksort animations 17
  • 18. Visual representation of insertion sort left of pointer is in sorted order right of pointer is untouched a[i] i Reason it is slow: data movement 18
  • 19. Shellsort Idea: move elements more than one position at a time by h-sorting the file for a decreasing sequence of values of h input S O R T E X A M P L E 1-sort A E L E O P M S X R T 7-sort A E L E O P M S X R T M O R T E X A S P L E A E E L O P M S X R T M O R T E X A S P L E A E E L O P M S X R T M O L T E X A S P R E A E E L O P M S X R T M O L E E X A S P R T A E E L M O P S X R T 3-sort A E E L M O P S X R T E O L M E X A S P R T A E E L M O P S X R T E E L M O X A S P R T A E E L M O P R S X T E E L M O X A S P R T A E E L M O P R S T X A E L E O X M S P R T A E E L M O P R S T X A E L E O X M S P R T A E L E O P M S X R T result A E E L M O P R S T X A E L E O P M S X R T A E L E O P M S X R T a 3-sorted file is A E L E O P M S X R T 3 interleaved sorted files A E M R E O S T L P X 19
  • 20. Shellsort Idea: move elements more than one position at a time by h-sorting the file for a decreasing sequence of values of h Use insertion sort, modified to h-sort magic increment sequence big increments: public static void sort(double[] a) small subfiles { int N = a.length; small increments: int[] incs = { 1391376, 463792, 198768, 86961, subfiles nearly in order 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1 }; for (int k = 0; k < incs.length; k++) method of choice for both { int h = incs[k]; small subfiles for (int i = h; i < N; i++) subfiles nearly in order for (int j = i; j >= h; j-= h) if (less(a[j], a[j-h])) insertion sort! exch(a, j, j-h); else break; } } 20
  • 21. Visual representation of shellsort big increment small increment Bottom line: substantially faster! 21
  • 22. Analysis of shellsort Model has not yet been discovered (!) N comparisons N1.289 2.5 N lg N 5,000 93 58 106 10,000 209 143 230 20,000 467 349 495 40,000 1022 855 1059 80,000 2266 2089 2257 measured in thousands 22
  • 23. Why are we interested in shellsort? Example of simple idea leading to substantial performance gains Useful in practice • fast unless file size is huge • tiny, fixed footprint for code (used in embedded systems) • hardware sort prototype Simple algorithm, nontrivial performance, interesting questions • asymptotic growth rate? • best sequence of increments? • average case performance? Your first open problem in algorithmics (see Section 6.8): Find a better increment sequence mail rs@cs.princeton.edu Lesson: some good algorithms are still waiting discovery 23
  • 24. rules of the game shellsort mergesort quicksort animations 24
  • 25. Mergesort (von Neumann, 1945(!)) Basic plan: plan • Divide array into two halves. M E R G E S O R T E X A M P L E • Recursively sort each half. E E G M O R R S T E X A M P L E • E E G M O R R S A E E L M P T X Merge two halves. A E E E E G L M M O P R R S T X trace a[i] lo hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 M E R G E S O R T E X A M P L E 0 1 E M R G E S O R T E X A M P L E 2 3 E M G R E S O R T E X A M P L E 0 3 E G M R E S O R T E X A M P L E 4 5 E G M R E S O R T E X A M P L E 6 7 E G M R E S O R T E X A M P L E 4 7 E G M R E O R S T E X A M P L E 0 7 E E G M O R R S T E X A M P L E 8 9 E E G M O R R S E T X A M P L E 10 11 E E G M O R R S E T A X M P L E 8 11 E E G M O R R S A E T X M P L E 12 13 E E G M O R R S A E T X M P L E 14 15 E E G M O R R S A E T X M P E L 12 15 E E G M O R R S A E T X E L M P 8 15 E E G M O R R S A E E L M P T X 0 15 A E E E E G L M M O P R R S T X 25
  • 26. Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? Use an auxiliary array. l i m j r aux[] A G L O R H I M S T k a[] A G H I L M private static void merge(Comparable[] a, Comparable[] aux, int l, int m, int r) { copy for (int k = l; k < r; k++) aux[k] = a[k]; int i = l, j = m; for (int k = l; k < r; k++) if (i >= m) a[k] = aux[j++]; see book for a trick merge else if (j >= r) a[k] = aux[i++]; to eliminate these else if (less(aux[j], aux[i])) a[k] = aux[j++]; else a[k] = aux[i++]; } 26
  • 27. Mergesort: Java implementation of recursive sort public class Merge { private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) { if (hi <= lo + 1) return; int m = lo + (hi - lo) / 2; sort(a, aux, lo, m); sort(a, aux, m, hi); merge(a, aux, lo, m, hi); } public static void sort(Comparable[] a) { Comparable[] aux = new Comparable[a.length]; sort(a, aux, 0, a.length); } } lo m hi 10 11 12 13 14 15 16 17 18 19 27
  • 28. Mergesort analysis: Memory Q. How much memory does mergesort require? A. Too much! • Original input array = N. • Auxiliary array for merging = N. • Local variables: constant. • Function call stack: log2 N [stay tuned]. • Total = 2N + O(log N). cannot “fill the memory and sort” Q. How much memory do other sorting algorithms require? • N + O(1) for insertion sort and selection sort. • In-place = N + O(log N). Challenge for the bored. In-place merge. [Kronrud, 1969] 28
  • 29. Mergesort analysis Def. T(N) number of array stores to mergesort an input of size N = T(N/2) + T(N/2) + N left half right half merge Mergesort recurrence T(N) = 2 T(N/2) + N for N > 1, with T(1) = 0 • not quite right for odd N • same recurrence holds for many algorithms • same for any input of size N • comparison count slightly smaller because of array ends lg N log2 N Solution of Mergesort recurrence T(N) ~ N lg N • true for all N • easy to prove when N is a power of 2 29
  • 30. Mergesort recurrence: Proof 1 (by recursion tree) T(N) = 2 T(N/2) + N (assume that N is a power of 2) for N > 1, with T(1) = 0 T(N) + T(N/2) T(N/2) N = N T(N/4) T(N/4) T(N/4) T(N/4) 2(N/2) = N ... lg N T(N / 2k) 2k(N/2k) = N ... T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) N/2 (2) = N N lg N T(N) = N lg N 30
  • 31. Mergesort recurrence: Proof 2 (by telescoping) T(N) = 2 T(N/2) + N (assume that N is a power of 2) for N > 1, with T(1) = 0 Pf. T(N) = 2 T(N/2) + N given T(N)/N = 2 T(N/2)/N + 1 divide both sides by N = T(N/2)/(N/2) + 1 algebra = T(N/4)/(N/4) + 1 + 1 telescope (apply to first term) = T(N/8)/(N/8) + 1 + 1 + 1 telescope again ... = T(N/N)/(N/N) + 1 + 1 +. . .+ 1 stop telescoping, T(1) = 0 = lg N T(N) = N lg N 31
  • 32. Mergesort recurrence: Proof 3 (by induction) T(N) = 2 T(N/2) + N (assume that N is a power of 2) for N > 1, with T(1) = 0 Claim. If T(N) satisfies this recurrence, then T(N) = N lg N. Pf. [by induction on N] • Base case: N = 1. • Inductive hypothesis: T(N) = N lg N • Goal: show that T(2N) + 2N lg (2N). T(2N) = 2 T(N) + 2N given = 2 N lg N + 2 N inductive hypothesis = 2 N (lg (2N) - 1) + 2N algebra = 2 N lg (2N) QED Ex. (for COS 340). Extend to show that T(N) ~ N lg N for general N 32
  • 33. Bottom-up mergesort Basic plan: • Pass through file, merging to double size of sorted subarrays. • Do so for subarray sizes 1, 2, 4, 8, . . . , N/2, N. proof 4 that mergesort uses N lgN compares a[i] lo hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 M E R G E S O R T E X A M P L E 0 1 E M R G E S O R T E X A M P L E 2 3 E M G R E S O R T E X A M P L E 4 5 E M G R E S O R T E X A M P L E 6 7 E M G R E S O R T E X A M P L E 8 9 E M G R E S O R E T X A M P L E 10 11 E M G R E S O R E T A X M P L E 12 13 E M G R E S O R E T A X M P L E 14 15 E M G R E S O R E T A X M P E L 0 3 E G M R E S O R E T A X M P E L 4 7 E G M R E O R S E T A X M P E L 8 11 E E G M O R R S A E T X M P E L 12 15 E E G M O R R S A E T X E L M P 0 7 E E G M O R R S A E T X E L M P 8 15 E E G M O R R S A E E L M P T X 0 15 A E E E E G L M M O P R R S T X No recursion needed! 33
  • 34. Bottom-up Mergesort: Java implementation public class Merge { private static void merge(Comparable[] a, Comparable[] aux, int l, int m, int r) { for (int i = l; i < m; i++) aux[i] = a[i]; for (int j = m; j < r; j++) aux[j] = a[m + r - j - 1]; tricky merge int i = l, j = r - 1; that uses sentinel for (int k = l; k < r; k++) (see Program 8.2) if (less(aux[j], aux[i])) a[k] = aux[j--]; else a[k] = aux[i++]; } public static void sort(Comparable[] a) { int N = a.length; Comparable[] aux = new Comparable[N]; for (int m = 1; m < N; m = m+m) for (int i = 0; i < N-m; i += m+m) merge(a, aux, i, i+m, Math.min(i+m+m, N)); } } Concise industrial-strength code if you have the space 34
  • 35. Mergesort: Practical Improvements Use sentinel. • Two statements in inner loop are array-bounds checking. • Reverse one subarray so that largest element is sentinel (Program 8.2) Use insertion sort on small subarrays. • Mergesort has too much overhead for tiny subarrays. • Cutoff to insertion sort for 7 elements. Stop if already sorted. • Is biggest element in first half smallest element in second half? • Helps for nearly ordered lists. Eliminate the copy to the auxiliary array. Save time (but not space) by switching the role of the input and auxiliary array in each recursive call. See Program 8.4 (or Java system sort) 35
  • 36. Sorting Analysis Summary Running time estimates: • Home pc executes 108 comparisons/second. • Supercomputer executes 1012 comparisons/second. Insertion Sort (N2) Mergesort (N log N) computer thousand million billion thousand million billion home instant 2.8 hours 317 years instant 1 sec 18 min super instant 1 second 1.6 weeks instant instant instant Lesson. Good algorithms are better than supercomputers. Good enough? 18 minutes might be too long for some applications 36
  • 37. rules of the game shellsort mergesort quicksort animations 37
  • 38. Quicksort (Hoare, 1959) Basic plan. • Shuffle the array. • Partition so that, for some i element a[i] is in place no larger element to the left of i no smaller element to the right of i • Sort each piece recursively. Sir Charles Antony Richard Hoare 1980 Turing Award input Q U I C K S O R T E X A M P L E randomize E R A T E S L P U I M Q C X O K partition E C A I E K L P U T M Q R X O S sort left part A C E E I K L P U T M Q R X O S sort right part A C E E I K L M O P Q R S T U X A C E E I K L M O P Q R S T U X result 38
  • 39. Quicksort: Java code for recursive sort public class Quick { public static void sort(Comparable[] a) { StdRandom.shuffle(a); sort(a, 0, a.length - 1); } private static void sort(Comparable[] a, int l, int r) { if (r <= l) return; int m = partition(a, l, r); sort(a, l, m-1); sort(a, m+1, r); } } 39
  • 40. Quicksort trace a[i] input l r i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Q U I C K S O R T E X A M P L E randomize E R A T E S L P U I M Q C X O K partition 0 15 5 E C A I E K L P U T M Q R X O S 0 4 2 A C E I E K L P U T M Q R X O S 0 1 1 A C E I E K L P U T M Q R X O S 0 0 A C E I E K L P U T M Q R X O S 3 4 3 A C E E I K L P U T M Q R X O S 4 4 A C E E I K L P U T M Q R X O S 6 15 12 A C E E I K L P O R M Q S X U T 6 11 10 A C E E I K L P O M Q R S X U T 6 9 7 A C E E I K L M O P Q R S X U T no partition for 6 6 A C E E I K L M O P Q R S X U T subfiles of size 1 8 9 9 A C E E I K L M O P Q R S X U T 8 8 A C E E I K L M O P Q R S X U T 11 11 A C E E I K L M O P Q R S X U T 13 15 13 A C E E I K L M O P Q R S T U X 14 15 15 A C E E I K L M O P Q R S T U X 14 14 A C E E I K L M O P Q R S T U X A C E E I K L M O P Q R S T U X array contents after each recursive sort 40
  • 41. Quicksort partitioning Basic plan: • scan from left for an item that belongs on the right • scan from right for item item that belongs on the left • exchange • continue until pointers cross a[i] i j r 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1 15 15 E R A T E S L P U I M Q C X O K scans 1 12 15 E R A T E S L P U I M Q C X O K exchange 1 12 15 E C A T E S L P U I M Q R X O K 3 9 15 E C A T E S L P U I M Q R X O K 3 9 15 E C A I E S L P U T M Q R X O K 5 5 15 E C A I E S L P U T M Q R X O K 5 5 15 E C A I E K L P U T M Q R X O S E C A I E K L P U T M Q R X O S array contents before and after each exchange 41
  • 42. Quicksort: Java code for partitioning private static int partition(Comparable[] a, int l, int r) { int i = l - 1; int j = r; v while(true) { i j find item on left to swap while (less(a[++i], a[r])) if (i == r) break; while (less(a[r], a[--j])) find item on right to swap if (j == l) break; <= v >= v v if (i >= j) break; check if pointers cross i j exch(a, i, j); swap } exch(a, i, r); swap with partitioning item <= v v >= v return i; return index of item now known to be in place } i 42
  • 43. Quicksort Implementation details Partitioning in-place. Using a spare array makes partitioning easier, but is not worth the cost. Terminating the loop. Testing whether the pointers cross is a bit trickier than it might seem. Staying in bounds. The (i == r) test is redundant, but the (j == l) test is not. Preserving randomness. Shuffling is key for performance guarantee. Equal keys. When duplicates are present, it is (counter-intuitively) best to stop on elements equal to partitioning element. 43
  • 44. Quicksort: Average-case analysis Theorem. The average number of comparisons CN to quicksort a random file of N elements is about 2N ln N. • The precise recurrence satisfies C 0 = C1 = 0 and for N 2: CN = N + 1 + ((C0 + CN-1) + . . . + (Ck-1 + CN-k) + . . . + (CN-1 + C1)) / N partition left right partitioning probability = N + 1 + 2 (C0 . . . + Ck-1 + . . . + CN-1) / N • Multiply both sides by N NCN = N(N + 1) + 2 (C0 . . . + Ck-1 + . . . + CN-1) • Subtract the same formula for N-1: NCN - (N - 1)CN-1 = N(N + 1) - (N - 1)N + 2 CN-1 • Simplify: NCN = (N + 1)CN-1 + 2N 44
  • 45. Quicksort: Average Case NCN = (N + 1)CN-1 + 2N • Divide both sides by N(N+1) to get a telescoping sum: CN / (N + 1) = CN-1 / N + 2 / (N + 1) = CN-2 / (N - 1) + 2/N + 2/(N + 1) = CN-3 / (N - 2) + 2/(N - 1) + 2/N + 2/(N + 1) = 2 ( 1 + 1/2 + 1/3 + . . . + 1/N + 1/(N + 1) ) • Approximate the exact answer by an integral: CN 2(N + 1)( 1 + 1/2 + 1/3 + . . . + 1/N ) N = 2(N + 1) HN 2(N + 1) dx/x 1 • Finally, the desired result: CN 2(N + 1) ln N 1.39 N lg N 45
  • 46. Quicksort: Summary of performance characteristics Worst case. Number of comparisons is quadratic. •N + (N-1) + (N-2) + … + 1 N2 / 2. •More likely that your computer is struck by lightning. Average case. Number of comparisons is ~ 1.39 N lg N. • 39% more comparisons than mergesort. • but faster than mergesort in practice because of lower cost of other high-frequency operations. Random shuffle • probabilistic guarantee against worst case • basis for math model that can be validated with experiments Caveat emptor. Many textbook implementations go quadratic if input: • Is sorted. • Is reverse sorted. • Has many duplicates (even if randomized)! [stay tuned] 46
  • 47. Sorting analysis summary Running time estimates: • Home pc executes 108 comparisons/second. • Supercomputer executes 1012 comparisons/second. Insertion Sort (N2) Mergesort (N log N) computer thousand million billion thousand million billion home instant 2.8 hours 317 years instant 1 sec 18 min super instant 1 second 1.6 weeks instant instant instant Quicksort (N log N) thousand million billion instant 0.3 sec 6 min instant instant instant Lesson 1. Good algorithms are better than supercomputers. Lesson 2. Great algorithms are better than good ones. 47
  • 48. Quicksort: Practical improvements Median of sample. • Best choice of pivot element = median. • But how to compute the median? • Estimate true median by taking median of sample. Insertion sort small files. • Even quicksort has too much overhead for tiny files. • Can delay insertion sort until end. Optimize parameters. 12/7 N log N comparisons • Median-of-3 random elements. • Cutoff to insertion sort for 10 elements. Non-recursive version. • Use explicit stack. guarantees O(log N) stack size • Always sort smaller half first. All validated with refined math models and experiments 48
  • 49. rules of the game shellsort mergesort quicksort animations 49
  • 50. Mergesort animation merge in progress input done untouched merge in progress auxiliary array output 50
  • 51. Bottom-up mergesort animation merge in progress input this pass last pass merge in progress auxiliary array output 51
  • 52. Quicksort animation i first partition v second partition done j 52