SlideShare une entreprise Scribd logo
1  sur  49
Elementary Sorts



                                                                ‣    rules of the game
                                                                ‣    selection sort
                                                                ‣    insertion sort
                                                                ‣    sorting challenges
                                                                ‣    shellsort
Reference:
  Algorithms in Java, Chapter 6
                                                                 Except as otherwise noted, the content of this presentation
  http://www.cs.princeton.edu/algs4                              is licensed under the Creative Commons Attribution 2.5 License.




 Algorithms in Java, 4th Edition   · Robert Sedgewick and Kevin Wayne · Copyright © 2008             ·    September 2, 2009 2:43:34 AM
Sorting problem


Ex. Student record in a University.




Sort. Rearrange array of N objects into ascending order.




                                                           2
Sample sort client


Goal. Sort any type of data.
Ex 1. Sort random numbers in ascending order.




  public class Experiment                       % java Experiment 10
  {                                             0.08614716385210452
     public static void main(String[] args)     0.09054270895414829
                                                0.10708746304898642
     {
                                                0.21166190071646818
        int N = Integer.parseInt(args[0]);      0.363292849257276
        Double[] a = new Double[N];             0.460954145685913
        for (int i = 0; i < N; i++)             0.5340026311350087
           a[i] = StdRandom.uniform();          0.7216129793703496
        Insertion.sort(a);                      0.9003500354411443
                                                0.9293994908845686
        for (int i = 0; i < N; i++)
           StdOut.println(a[i]);
     }
  }




                                                                       3
Sample sort client


Goal. Sort any type of data.
Ex 2. Sort strings from standard input in alphabetical order.


   public class StringSort
   {
      public static void main(String[] args)
      {
         String[] a = StdIn.readAll().split("s+");
         Insertion.sort(a);
         for (int i = 0; i < N; i++)
            StdOut.println(a[i]);
      }
   }



                     % more words3.txt
                     bed bug dad dot zoo ... all bad bin

                     % java StringSort < words.txt
                     all bad bed bug dad ... yes yet zoo


                                                                4
Sample sort client


Goal. Sort any type of data.
Ex 3. Sort the files in a given directory by filename.




  import java.io.File;                                   % java Files .
  public class Files                                     Insertion.class
  {                                                      Insertion.java
                                                         InsertionX.class
     public static void main(String[] args)
                                                         InsertionX.java
     {                                                   Selection.class
        File directory = new File(args[0]);              Selection.java
        File[] files = directory.listFiles();            Shell.class
        Insertion.sort(files);                           Shell.java
        for (int i = 0; i < files.length; i++)           ShellX.class
                                                         ShellX.java
           StdOut.println(files[i]);
     }
  }




                                                                            5
Callbacks


Goal. Sort any type of data.


Q. How can sort know to compare data of type String, Double, and File
without any information about the type of an item?


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


Implementing callbacks.
•   Java: interfaces.
•   C: function pointers.
•   C++: class-type functors.
•   ML: first-class functions and functors.



                                                                        6
Callbacks: roadmap

client                                                                   object implementation
import java.io.File;                                                     public class File
public class SortFiles                                                   implements Comparable<File>
{                                                                        {
   public static void main(String[] args)                                   ...
   {                                                                        public int compareTo(File b)
      File directory = new File(args[0]);                                   {
      File[] files = directory.listFiles();                                     ...
      Insertion.sort(files);                                                    return -1;
      for (int i = 0; i < files.length; i++)                                    ...
         StdOut.println(files[i]);                                              return +1;
   }                                                                            ...
}                                                                               return 0;
                                                                            }
                                                                         }


                                              built in to Java
interface

  public interface Comparable<Item>                              sort implementation
  {                                                              public static void sort(Comparable[] a)
     public int compareTo(Item);                                 {
  }                                                                 int N = a.length;
                                                                    for (int i = 0; i < N; i++)
                                                                       for (int j = i; j > 0; j--)
                                                                          if (a[j].compareTo(a[j-1]))
                                                                               exch(a, j, j-1);
                                                                          else break;
                  Key point: no reference to File
                                                                 }

                                                                                                           7
Comparable interface API


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


               public interface Comparable<Item>
               {
                  public int compareTo(Item that);
               }




Consistency. Implementation must ensure a total order.
•   Transitivity: if (a < b) and (b < c), then (a < c).
•   Trichotomy: 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.
                                                                      8
Implementing the Comparable interface: example 1


Date data type. Simplified version of java.util.Date.

   public class Date implements Comparable<Date>
   {
      private final int month, day, year;

       public Date(int m, int d, int y)
       {
                                                        only compare dates
          month = m;                                    to other dates
          day   = d;
          year = y;
       }

       public int compareTo(Date that)
       {
          if (this.year < that.year )    return   -1;
          if (this.year > that.year )    return   +1;
          if (this.month < that.month)   return   -1;
          if (this.month > that.month)   return   +1;
          if (this.day   < that.day )    return   -1;
          if (this.day   > that.day )    return   +1;
          return 0;
       }
   }
                                                                             9
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.
                                                                 subdomains
     public class Domain implements Comparable<Domain>
     {                                                           ee.princeton.edu
        private final String[] fields;                           cs.princeton.edu
        private final int N;                                     princeton.edu
                                                                 cnn.com
         public Domain(String name)
                                                                 google.com
         {
                                                                 apple.com
             fields = name.split(".");
             N = fields.length;                                  www.cs.princeton.edu
         }                                                       bolle.cs.princeton.edu

         public int compareTo(Domain that)
         {                                                       reverse-sorted subdomains
            for (int i = 0; i < Math.min(this.N, that.N); i++)
            {                                                    com.apple
               String s = fields[this.N - i - 1];                com.cnn
               String t = fields[that.N - i - 1];                com.google
               int cmp = s.compareTo(t);
                                                                 edu.princeton
               if      (cmp < 0) return -1;
                                                                 edu.princeton.cs
               else if (cmp > 0) return +1;
            }
                                                                 edu.princeton.cs.bolle
            return this.N - that.N;                              edu.princeton.cs.www
         }                                                       edu.princeton.ee
     }
                                                                                             10
Two useful sorting abstractions


Helper functions. Refer to data through compares and exchanges.


Less. Is object v less than w ?


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




Exchange. Swap object in array a[] 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;
     }

                                                                         11
Testing


Q. How to test if an array is sorted?



     private static boolean isSorted(Comparable[] a)
     {
        for (int i = 1; i < a.length; i++)
           if (less(a[i], a[i-1])) return false;
        return true;
     }




Q. If the sorting algorithm passes the test, did it correctly sort its input?
A1. Not necessarily!
A2. Yes, if data accessed only through exch() and less().




                                                                                12
‣   rules of the game
‣   selection sort
‣   insertion sort
‣   sorting challenges
‣   shellsort




                         13
Selection sort


Algorithm. ↑ scans from left to right.


Invariants.
•   Elements to the left of ↑ (including ↑) fixed and in ascending order.
•   No element to right of ↑ is smaller than any element to its left.




                                        ↑
                       in final order

                                                                            14
Selection sort inner loop


To maintain algorithm invariants:


•   Move the pointer to the right.


       i++;
                                               in final order    ↑

•   Identify index of minimum item on right.


       int min = i;
       for (int j = i+1; j < N; j++)
          if (less(a[j], a[min]))
                                                in final order   ↑   ↑
             min = j;




•   Exchange into position.


       exch(a, i, min);
                                               in final order    ↑   ↑
                                                                         15
Selection sort: Java implementation



       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], a[min]))
                       min = j;
                 exch(a, i, min);
              }
           }

           private boolean less(Comparable v, Comparable w)
           { /* as before */ }

           private boolean exch(Comparable[] a, int i, int j)
           { /* as before */ }
       }


                                                                16
Selection sort: mathematical analysis


Proposition A. Selection sort uses (N-1) + (N-2) + ... + 1 + 0 ~ N2/2
compares and N exchanges.


                                      a[]
                                                                    entries in black
         i min      0   1   2   3   4 5 6       7   8   9 10     are examined to find
                                                                     the minimum
                    S   O   R   T   E   X   A   M   P   L   E
         0     6    S   O   R   T   E   X   A   M   P   L   E
                                                                    entries in red
         1     4    A   O   R   T   E   X   S   M   P   L   E        are a[min]
         2    10    A   E   R   T   O   X   S   M   P   L   E
         3     9    A   E   E   T   O   X   S   M   P   L   R
         4     7    A   E   E   L   O   X   S   M   P   T   R
         5     7    A   E   E   L   M   X   S   O   P   T   R
         6     8    A   E   E   L   M   O   S   X   P   T   R
         7    10    A   E   E   L   M   O   P   X   S   T   R
         8     8    A   E   E   L   M   O   P   R   S   T   X
                                                                  entries in gray are
         9     9    A   E   E   L   M   O   P   R   S   T   X      in final position
        10    10    A   E   E   L   M   O   P   R   S   T   X
                    A   E   E   L   M   O   P   R   S   T   X

             Trace of selection sort (array contents just after each exchange)

Running time insensitive to input. Quadratic time, even if array is presorted.
Data movement is minimal. Linear number of exchanges.
                                                                                        17
‣   rules of the game
‣   selection sort
‣   insertion sort
‣   sorting challenges
‣   shellsort




                         18
Insertion sort


Algorithm. ↑ scans from left to right.


Invariants.
•   Elements to the left of ↑ (including ↑) are in ascending order.
•   Elements to the right of ↑ have not yet been seen.




                    in order   ↑            not yet seen




                                                                      19
Insertion sort inner loop


To maintain algorithm invariants:


•   Move the pointer to the right.


       i++;

                                                              ↑

                                                   in order        not yet seen



•   Moving from right to left, exchange
    a[i]   with each larger element to its left.



       for (int j = i; j > 0; j--)
          if (less(a[j], a[j-1]))
               exch(a, j, j-1);
          else break;                                ↑ ↑ ↑↑

                                                   in order       not yet seen

                                                                                  20
Insertion sort: Java implementation




       public class Insertion {

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

           private boolean less(Comparable v, Comparable w)
           { /* as before */ }

           private boolean exch(Comparable[] a, int i, int j)
           { /* as before */ }
       }




                                                                21
Insertion sort: mathematical analysis


Proposition B. For randomly-ordered data with distinct keys, insertion sort
uses ~ N2/4 compares and N2/4 exchanges on the average.


Pf. For randomly data, we expect each element to move halfway back.


                                         a[]
            i     j    0   1   2   3   4 5 6       7   8   9 10
                       S   O   R   T   E   X   A   M   P   L   E       entries in gray
                                                                        do not move
            1     0    O   S   R   T   E   X   A   M   P   L   E
            2     1    O   R   S   T   E   X   A   M   P   L   E
            3     3    O   R   S   T   E   X   A   M   P   L   E
            4     0    E   O   R   S   T   X   A   M   P   L   E       entry in red
                                                                         is a[j]
            5     5    E   O   R   S   T   X   A   M   P   L   E
            6     0    A   E   O   R   S   T   X   M   P   L   E
            7     2    A   E   M   O   R   S   T   X   P   L   E
                                                                      entries in black
            8     4    A   E   M   O   P   R   S   T   X   L   E     moved one position
            9     2    A   E   L   M   O   P   R   S   T   X   E     right for insertion
           10     2    A   E   E   L   M   O   P   R   S   T   X
                       A   E   E   L   M   O   P   R   S   T   X

                Trace of insertion sort (array contents just after each insertion)

                                                                                           22
Insertion sort: best and worst case


Best case. If the input is in ascending order, insertion sort makes
N-1 compares and 0 exchanges.


              A E E L M O P R S T X




Worst case. If the input is in descending order (and no duplicates),
insertion sort makes ~ N2/2 compares and ~ N2/2 exchanges.


              X T S R P O M L E E A




                                                                       23
Insertion sort: partially sorted inputs


Def. An inversion is a pair of keys that are out of order.

                A E E L M O T R X P S


                  T-R T-P T-S X-P X-S
                         (5 inversions)




Def. An array is partially sorted if the number of inversions is O(N).
•   Ex 1. A small array appended to a large sorted array.
•   Ex 2. An array with only a few elements out of place.



Proposition C. For partially-sorted arrays, insertion sort runs in linear time.
Pf. Number of exchanges equals the number of inversions.

                  number of compares = exchanges + (N-1)


                                                                                  24
‣   rules of the game
‣   selection sort
‣   insertion sort
‣   sorting challenges
‣   shellsort




                         25
Rather than tracing the progress of a sort with key values such as letters, numbers or
      Sorting challenge 0
words, we use vertical bars, to be sorted by their heights. As you will see, the advantage
of such a representation is that it can give insights into
the behavior of a sorting method.
      Input. Array of doubles.
         For example, you can see at a glance on the vi-
sual traces at rightproportional to length.not touch
      Plot. Data that insertion sort does
entries to the right of the scan pointer and selection
sort does not touch entries to the left of the scan point-                      gray entries
      Name the clear from the visual traces that, since
                    sorting method.
                                                                               are untouched
er. Moreover, it is
      • Insertion sort.
insertion sort also does not touch entries smaller than

      • Selection sort.
the inserted element, it uses about half the number of
compares as selection sort, on the average.
         With our StdDraw library, developing a visual
trace is not much more difficult than doing a standard
trace. We sort Double values, instrument the algorithm
to call show() as appropriate (just as we do for a stan-
dard trace) and develop a version of show() that uses
StdDraw to draw the bars instead of printing the results.
The most complicated task is setting the scale for the y
axis so that the lines of the trace appear in the expected                      black entries
order. You are encouraged to work EXERCISE 3.1.19 in                            are involved
                                                                                in compares
order to gain a better appreciation of the value of visual
traces and the ease of creating them.
         An even simpler task is to animate the trace
so that you can see the array dynamically evolve to the
sorted result. Developing an animated trace involves
essentially the same process described in the previous          insertion sort                selection sort
                                                                                                               26
Sorting challenge 1


Problem. Sort a file of huge records with tiny keys.
Ex. Reorganize your MP3 files.


Which sorting method to use?
•   System sort.
•   Insertion sort.
•   Selection sort.




                                                       27
Sorting challenge 1


Problem. Sort a file of huge records with tiny keys.
Ex. Reorganize your MP3 files.


Which sorting method to use?
•   System sort.            probably no, selection sort simpler and faster
•   Insertion sort.         no, too many exchanges
•   Selection sort.         yes, linear time under reasonable assumptions



Ex: 5,000 records, each 2 million bytes with 100-byte keys.

    Cost of comparisons: 100 × 50002 / 2 = 1.25 billion.

    Cost of exchanges: 2,000,000 × 5,000 = 10 trillion.

    System sort might be a factor of log (5000) slower.




                                                                             28
Sorting challenge 2


Problem. Sort a huge randomly-ordered file of small records.
Ex. Process transaction records for a phone company.


Which sorting method to use?
•   System sort.
•   Insertion sort.
•   Selection sort.




                                                               29
Sorting challenge 2


Problem. Sort a huge randomly-ordered file of small records.
Ex. Process transaction records for a phone company.


Which sorting method to use?
•   System sort.          yes, it's designed for this problem
•   Insertion sort.       no, quadratic time for randomly ordered files
•   Selection sort.       no, always quadratic time




                                                                          30
Sorting challenge 3


Problem. Sort a huge number of tiny files (each file is independent)
Ex. Daily customer transaction records.


Which sorting method to use?
•   System sort.
•   Insertion sort.
•   Selection sort.




                                                                       31
Sorting challenge 3


Problem. Sort a huge number of tiny files (each file is independent)
Ex. Daily customer transaction records.


Which sorting method to use?
•   System sort.          no, too much overhead
•   Insertion sort.       yes, less overhead than system sort
•   Selection sort.       yes, less overhead than system sort



Ex: 4 record file.

    4 N log N + 35 = 70

    2N2 = 32




                                                                       32
Sorting challenge 4


Problem. Sort a huge file that is already almost in order.
Ex. Resort a huge database after a few changes.


Which sorting method to use?
•   System sort.
•   Insertion sort.
•   Selection sort.




                                                             33
Sorting challenge 4


Problem. Sort a huge file that is already almost in order.
Ex. Resort a huge database after a few changes.


Which sorting method to use?
•   System sort.           no, insertion sort simpler and faster
•   Insertion sort.        yes, linear time for most definitions of "in order"
•   Selection sort.        no, always takes quadratic time



Ex.
•   A B C D E F H I J G P K L M N O Q R S T U V W X Y Z
•   Z A B C D E F G H I J K L M N O P Q R S T U V W X Y




                                                                                 34
‣   rules of the game
‣   selection sort
‣   insertion sort
‣   animations
‣   shellsort




                        35
Insertion sort animation

                    left of pointer is in sorted order       right of pointer is untouched




  a[i]




                                                         i


                                                                                             36
Insertion sort animation




Reason it is slow: excessive data movement.
                                              37
Insertion sort animation




Reason it is slow: excessive data movement.
                                              37
move through the array only one place at a time. For exa
Shellsort overview                     smallest key happens to be at the end of the array, N steps are
                                       ment where it belongs. Shellsort is a simple extension of ins
                                       by allowing exchanges of elements that are far apart, to pro
Idea. Move elements more than one position at a time by h-sorting the file.
                                       that can be efficiently sorted, eventually by insertion sort.
                                                  The idea is to rearrange the array to give it the pro
                   a 3-sorted file is 3 interleaved sorted files       element (starting anywhere)
                      h=3                                              Such an array is said to be h-s
                        A E L E O P M S X R T                          h-sorted array is h independen
                        A             E             M            R     terleaved together. By h-sorting
                            E              O            S          T   we can move elements in the ar
                                 L             P             X         make it easier to h-sort for sma
                                                                       a procedure for any increment
                      h=7
                                                                       ends in 1 will produce a sorted
                        M O L E E X A S P R T
                                                                                One way to impleme
Shellsort. h-sort the M file for a decreasing S          sequence of values of to use insertion sort
                                                                       each h, h.
                            E                                P         the h subsequences. Despite
                                 L                               R
                                                                       this process, we can use an ev
                                      E                            T
                      input S O R T E X A M P L E                      cisely because the subsequenc
                                           E
                      7-sort M O L E E X A S P R T
                                               L
                                                                       h-sorting the array, we simply
                      3-sort A E L E O P M S X R T  A
                                                                       the previous elements in its
                                                                       larger elements to the right. W
                      An h-sorted file is h M O P R sorted files using the insertion-sort code,
                      1-sort A E E L interleaved S T X

                      Shellsort trace (array contents after each pass) or decrement by h instead of 1
                                       array. This observation reduces the shellsort implementatio
                                                                                                  38
h-sorting


How to h-sort a file? Insertion sort, with stride length h.


                    3-sorting a file


                    M    O    L    E   E   X   A   S   P   R   T
                    E    O    L    M   E   X   A   S   P   R   T
                    E    E    L    M   O   X   A   S   P   R   T
                    E    E    L    M   O   X   A   S   P   R   T
                    A    E    L    E   O   X   M   S   P   R   T
                    A    E    L    E   O   X   M   S   P   R   T
                    A    E    L    E   O   P   M   S   X   R   T
                    A    E    L    E   O   P   M   S   X   R   T
                    A    E    L    E   O   P   M   S   X   R   T
                    A    E    L    E   O   P   M   S   X   R   T




Why insertion sort?
•   Big increments ⇒ small subfiles.
•   Small increments ⇒ nearly in order. [stay tuned]


                                                                   39
Shellsort example



input                                                1-sort

         S   O   R   T   E   X   A   M   P   L   E            A   E   L   E   O   P   M   S   X   R   T
                                                              A   E   L   E   O   P   M   S   X   R   T
                                                              A   E   L   E   O   P   M   S   X   R   T
7-sort                                                        A   E   E   L   O   P   M   S   X   R   T
         S   O   R   T   E   X   A   M   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   R   T   E   X   A   S   P   L   E            A   E   E   L   M   O   P   S   X   R   T
         M   O   L   T   E   X   A   S   P   R   E            A   E   E   L   M   O   P   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
                                                              A   E   E   L   M   O   P   R   S   X   T
                                                              A   E   E   L   M   O   P   R   S   T   X
3-sort
         M   O   L   E   E   X   A   S   P   R   T
         E   O   L   M   E   X   A   S   P   R   T
         E   E   L   M   O   X   A   S   P   R   T   result

         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   L   E   O   X   M   S   P   R   T
         A   E   L   E   O   P   M   S   X   R   T
         A   E   L   E   O   P   M   S   X   R   T
         A   E   L   E   O   P   M   S   X   R   T

                                                                                                          40
Shellsort: Java implementation


      public class Shell
      {
         public static void sort(Comparable[] a)
         {
            int N = a.length;
            int[] incs = { 1391376, 463792, 198768, 86961,
                            33936, 13776, 4592, 1968, 861,     magic increment
                            336, 112, 48, 21, 7, 3, 1             sequence
                          };
            for (int k = 0; k < incs.length; k++)
            {
               int h = incs[k];
               for (int i = h; i < N; i++)
                  for (int j = i; j >= h; j-= h)
                     if (less(a[j], a[j-h]))                   insertion sort
                         exch(a, j, j-h);
                     else break;
            }
         }

          private boolean less(Comparable v, Comparable w)
          { /* as before */ }

          private boolean exch(Comparable[] a, int i, int j)
          { /* as before */ }
      }
                                                                                 41
Visual trace of shellsort

             input




             112-sorted




             48-sorted




             21-sorted




             7-sorted




             3-sorted




             result




                            42
Shellsort animation



 big increment




                      small increment




                                        43
Shellsort animation




Bottom line: substantially faster than insertion sort!
                                                         44
Shellsort animation




Bottom line: substantially faster than insertion sort!
                                                         44
Empirical analysis of shellsort


Property. The number of compares used by shellsort with the increments 1, 4,
13, 40, ... is at most by a small multiple of N times the # of increments used.



                      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




Remark. Accurate model has not yet been discovered (!)
                                                                                  45
Shellsort: mathematical analysis


Proposition. A g-sorted array remains g-sorted after h-sorting it.
Pf. Harder than you'd think!


 7-sort                                           3-sort

  M       O   R   T   E   X   A   S   P   L   E   M    O   L   E   E     X    A    S    P   R   T
  M       O   R   T   E   X   A   S   P   L   E   E    O   L   M   E     X    A    S    P   R   T
  M       O   L   T   E   X   A   S   P   R   E   E    E   L   M   O     X    A    S    P   R   T
  M       O   L   E   E   X   A   S   P   R   T   E    E   L   M   O     X    A    S    P   R   T
  M       O   L   E   E   X   A   S   P   R   T   A    E   L   E   O     X    M    S    P   R   T
                                                  A    E   L   E   O     X    M    S    P   R   T
                                                  A    E   L   E   O     P    M    S    X   R   T
                                                  A    E   L   E   O     P    M    S    X   R   T
                                                  A    E   L   E   O     P    M    S    X   R   T
                                                  A    E   L   E   O     P    M    S    X   R   T

                                                                       still 7-sorted




Proposition. The worst-case number of compares for shellsort using
the 3x+1 increment sequence 1, 4, 13, 40, 121, 364, … is O(N3/2).
                                                                                                    46
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?         open problem: find a better increment sequence




Lesson. Some good algorithms are still waiting discovery.



                                                                                       47

Contenu connexe

Tendances

Java Collections API
Java Collections APIJava Collections API
Java Collections APIAlex Miller
 
The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31Mahmoud Samir Fayed
 
Joose @jsconf
Joose @jsconfJoose @jsconf
Joose @jsconfmalteubl
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Agora Group
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 

Tendances (20)

Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31
 
Joose @jsconf
Joose @jsconfJoose @jsconf
Joose @jsconf
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Generics
GenericsGenerics
Generics
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Collection framework
Collection frameworkCollection framework
Collection framework
 

En vedette

8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertionirdginfo
 
Topic11 sortingandsearching
Topic11 sortingandsearchingTopic11 sortingandsearching
Topic11 sortingandsearchingGopi Saiteja
 
Makalah pencarian dan pengurutan data
Makalah pencarian dan pengurutan dataMakalah pencarian dan pengurutan data
Makalah pencarian dan pengurutan dataAli Must Can
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort Mahesh Dheravath
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort AlgorithmGail Carmichael
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting AlgorithmsDamian T. Gordon
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 

En vedette (11)

8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
 
Topic11 sortingandsearching
Topic11 sortingandsearchingTopic11 sortingandsearching
Topic11 sortingandsearching
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Makalah pencarian dan pengurutan data
Makalah pencarian dan pengurutan dataMakalah pencarian dan pengurutan data
Makalah pencarian dan pengurutan data
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 

Similaire à Elementary Sort

Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaSandesh Sharma
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntuKhurshid Asghar
 
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24  tricky stuff in java grammar and javacJug trojmiasto 2014.04.24  tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javacAnna Brzezińska
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdffantoosh1
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartGabriele Lana
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic SyntaxAdil Jafri
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.pptKhizar40
 

Similaire à Elementary Sort (20)

Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
core java
core javacore java
core java
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Python tour
Python tourPython tour
Python tour
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 
Java ppt
Java pptJava ppt
Java ppt
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24  tricky stuff in java grammar and javacJug trojmiasto 2014.04.24  tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
1_JavIntro
1_JavIntro1_JavIntro
1_JavIntro
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdf
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 

Plus de Sri Prasanna

Plus de Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 
Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 

Dernier

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Dernier (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Elementary Sort

  • 1. Elementary Sorts ‣ rules of the game ‣ selection sort ‣ insertion sort ‣ sorting challenges ‣ shellsort Reference: Algorithms in Java, Chapter 6 Except as otherwise noted, the content of this presentation http://www.cs.princeton.edu/algs4 is licensed under the Creative Commons Attribution 2.5 License. Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September 2, 2009 2:43:34 AM
  • 2. Sorting problem Ex. Student record in a University. Sort. Rearrange array of N objects into ascending order. 2
  • 3. Sample sort client Goal. Sort any type of data. Ex 1. Sort random numbers in ascending order. public class Experiment % java Experiment 10 { 0.08614716385210452 public static void main(String[] args) 0.09054270895414829 0.10708746304898642 { 0.21166190071646818 int N = Integer.parseInt(args[0]); 0.363292849257276 Double[] a = new Double[N]; 0.460954145685913 for (int i = 0; i < N; i++) 0.5340026311350087 a[i] = StdRandom.uniform(); 0.7216129793703496 Insertion.sort(a); 0.9003500354411443 0.9293994908845686 for (int i = 0; i < N; i++) StdOut.println(a[i]); } } 3
  • 4. Sample sort client Goal. Sort any type of data. Ex 2. Sort strings from standard input in alphabetical order. public class StringSort { public static void main(String[] args) { String[] a = StdIn.readAll().split("s+"); Insertion.sort(a); for (int i = 0; i < N; i++) StdOut.println(a[i]); } } % more words3.txt bed bug dad dot zoo ... all bad bin % java StringSort < words.txt all bad bed bug dad ... yes yet zoo 4
  • 5. Sample sort client Goal. Sort any type of data. Ex 3. Sort the files in a given directory by filename. import java.io.File; % java Files . public class Files Insertion.class { Insertion.java InsertionX.class public static void main(String[] args) InsertionX.java { Selection.class File directory = new File(args[0]); Selection.java File[] files = directory.listFiles(); Shell.class Insertion.sort(files); Shell.java for (int i = 0; i < files.length; i++) ShellX.class ShellX.java StdOut.println(files[i]); } } 5
  • 6. Callbacks Goal. Sort any type of data. Q. How can sort know to compare data of type String, Double, and File without any information about the type of an item? Callbacks. • Client passes array of objects to sorting routine. • Sorting routine calls back object's compare function as needed. Implementing callbacks. • Java: interfaces. • C: function pointers. • C++: class-type functors. • ML: first-class functions and functors. 6
  • 7. Callbacks: roadmap client object implementation import java.io.File; public class File public class SortFiles implements Comparable<File> { { public static void main(String[] args) ... { public int compareTo(File b) File directory = new File(args[0]); { File[] files = directory.listFiles(); ... Insertion.sort(files); return -1; for (int i = 0; i < files.length; i++) ... StdOut.println(files[i]); return +1; } ... } return 0; } } built in to Java interface public interface Comparable<Item> sort implementation { public static void sort(Comparable[] a) public int compareTo(Item); { } int N = a.length; for (int i = 0; i < N; i++) for (int j = i; j > 0; j--) if (a[j].compareTo(a[j-1])) exch(a, j, j-1); else break; Key point: no reference to File } 7
  • 8. Comparable interface API Comparable interface. Implement compareTo() so that v.compareTo(w): • Returns a negative integer if v is less than w. • Returns a positive integer if v is greater than w. • Returns zero if v is equal to w. public interface Comparable<Item> { public int compareTo(Item that); } Consistency. Implementation must ensure a total order. • Transitivity: if (a < b) and (b < c), then (a < c). • Trichotomy: 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. 8
  • 9. Implementing the Comparable interface: example 1 Date data type. Simplified version of java.util.Date. public class Date implements Comparable<Date> { private final int month, day, year; public Date(int m, int d, int y) { only compare dates month = m; to other dates day = d; year = y; } public int compareTo(Date that) { if (this.year < that.year ) return -1; if (this.year > that.year ) return +1; if (this.month < that.month) return -1; if (this.month > that.month) return +1; if (this.day < that.day ) return -1; if (this.day > that.day ) return +1; return 0; } } 9
  • 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. subdomains public class Domain implements Comparable<Domain> { ee.princeton.edu private final String[] fields; cs.princeton.edu private final int N; princeton.edu cnn.com public Domain(String name) google.com { apple.com fields = name.split("."); N = fields.length; www.cs.princeton.edu } bolle.cs.princeton.edu public int compareTo(Domain that) { reverse-sorted subdomains for (int i = 0; i < Math.min(this.N, that.N); i++) { com.apple String s = fields[this.N - i - 1]; com.cnn String t = fields[that.N - i - 1]; com.google int cmp = s.compareTo(t); edu.princeton if (cmp < 0) return -1; edu.princeton.cs else if (cmp > 0) return +1; } edu.princeton.cs.bolle return this.N - that.N; edu.princeton.cs.www } edu.princeton.ee } 10
  • 11. Two useful sorting abstractions Helper functions. Refer to data through compares and exchanges. Less. Is object v less than w ? private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } Exchange. Swap object in array a[] 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; } 11
  • 12. Testing Q. How to test if an array is sorted? private static boolean isSorted(Comparable[] a) { for (int i = 1; i < a.length; i++) if (less(a[i], a[i-1])) return false; return true; } Q. If the sorting algorithm passes the test, did it correctly sort its input? A1. Not necessarily! A2. Yes, if data accessed only through exch() and less(). 12
  • 13. rules of the game ‣ selection sort ‣ insertion sort ‣ sorting challenges ‣ shellsort 13
  • 14. Selection sort Algorithm. ↑ scans from left to right. Invariants. • Elements to the left of ↑ (including ↑) fixed and in ascending order. • No element to right of ↑ is smaller than any element to its left. ↑ in final order 14
  • 15. Selection sort inner loop To maintain algorithm invariants: • Move the pointer to the right. i++; in final order ↑ • Identify index of minimum item on right. int min = i; for (int j = i+1; j < N; j++) if (less(a[j], a[min])) in final order ↑ ↑ min = j; • Exchange into position. exch(a, i, min); in final order ↑ ↑ 15
  • 16. Selection sort: Java implementation 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], a[min])) min = j; exch(a, i, min); } } private boolean less(Comparable v, Comparable w) { /* as before */ } private boolean exch(Comparable[] a, int i, int j) { /* as before */ } } 16
  • 17. Selection sort: mathematical analysis Proposition A. Selection sort uses (N-1) + (N-2) + ... + 1 + 0 ~ N2/2 compares and N exchanges. a[] entries in black i min 0 1 2 3 4 5 6 7 8 9 10 are examined to find the minimum S O R T E X A M P L E 0 6 S O R T E X A M P L E entries in red 1 4 A O R T E X S M P L E are a[min] 2 10 A E R T O X S M P L E 3 9 A E E T O X S M P L R 4 7 A E E L O X S M P T R 5 7 A E E L M X S O P T R 6 8 A E E L M O S X P T R 7 10 A E E L M O P X S T R 8 8 A E E L M O P R S T X entries in gray are 9 9 A E E L M O P R S T X in final position 10 10 A E E L M O P R S T X A E E L M O P R S T X Trace of selection sort (array contents just after each exchange) Running time insensitive to input. Quadratic time, even if array is presorted. Data movement is minimal. Linear number of exchanges. 17
  • 18. rules of the game ‣ selection sort ‣ insertion sort ‣ sorting challenges ‣ shellsort 18
  • 19. Insertion sort Algorithm. ↑ scans from left to right. Invariants. • Elements to the left of ↑ (including ↑) are in ascending order. • Elements to the right of ↑ have not yet been seen. in order ↑ not yet seen 19
  • 20. Insertion sort inner loop To maintain algorithm invariants: • Move the pointer to the right. i++; ↑ in order not yet seen • Moving from right to left, exchange a[i] with each larger element to its left. for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break; ↑ ↑ ↑↑ in order not yet seen 20
  • 21. Insertion sort: Java implementation public class Insertion { public static void sort(Comparable[] a) { int N = a.length; for (int i = 0; i < N; i++) for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break; } private boolean less(Comparable v, Comparable w) { /* as before */ } private boolean exch(Comparable[] a, int i, int j) { /* as before */ } } 21
  • 22. Insertion sort: mathematical analysis Proposition B. For randomly-ordered data with distinct keys, insertion sort uses ~ N2/4 compares and N2/4 exchanges on the average. Pf. For randomly data, we expect each element to move halfway back. a[] i j 0 1 2 3 4 5 6 7 8 9 10 S O R T E X A M P L E entries in gray do not move 1 0 O S R T E X A M P L E 2 1 O R S T E X A M P L E 3 3 O R S T E X A M P L E 4 0 E O R S T X A M P L E entry in red is a[j] 5 5 E O R S T X A M P L E 6 0 A E O R S T X M P L E 7 2 A E M O R S T X P L E entries in black 8 4 A E M O P R S T X L E moved one position 9 2 A E L M O P R S T X E right for insertion 10 2 A E E L M O P R S T X A E E L M O P R S T X Trace of insertion sort (array contents just after each insertion) 22
  • 23. Insertion sort: best and worst case Best case. If the input is in ascending order, insertion sort makes N-1 compares and 0 exchanges. A E E L M O P R S T X Worst case. If the input is in descending order (and no duplicates), insertion sort makes ~ N2/2 compares and ~ N2/2 exchanges. X T S R P O M L E E A 23
  • 24. Insertion sort: partially sorted inputs Def. An inversion is a pair of keys that are out of order. A E E L M O T R X P S T-R T-P T-S X-P X-S (5 inversions) Def. An array is partially sorted if the number of inversions is O(N). • Ex 1. A small array appended to a large sorted array. • Ex 2. An array with only a few elements out of place. Proposition C. For partially-sorted arrays, insertion sort runs in linear time. Pf. Number of exchanges equals the number of inversions. number of compares = exchanges + (N-1) 24
  • 25. rules of the game ‣ selection sort ‣ insertion sort ‣ sorting challenges ‣ shellsort 25
  • 26. Rather than tracing the progress of a sort with key values such as letters, numbers or Sorting challenge 0 words, we use vertical bars, to be sorted by their heights. As you will see, the advantage of such a representation is that it can give insights into the behavior of a sorting method. Input. Array of doubles. For example, you can see at a glance on the vi- sual traces at rightproportional to length.not touch Plot. Data that insertion sort does entries to the right of the scan pointer and selection sort does not touch entries to the left of the scan point- gray entries Name the clear from the visual traces that, since sorting method. are untouched er. Moreover, it is • Insertion sort. insertion sort also does not touch entries smaller than • Selection sort. the inserted element, it uses about half the number of compares as selection sort, on the average. With our StdDraw library, developing a visual trace is not much more difficult than doing a standard trace. We sort Double values, instrument the algorithm to call show() as appropriate (just as we do for a stan- dard trace) and develop a version of show() that uses StdDraw to draw the bars instead of printing the results. The most complicated task is setting the scale for the y axis so that the lines of the trace appear in the expected black entries order. You are encouraged to work EXERCISE 3.1.19 in are involved in compares order to gain a better appreciation of the value of visual traces and the ease of creating them. An even simpler task is to animate the trace so that you can see the array dynamically evolve to the sorted result. Developing an animated trace involves essentially the same process described in the previous insertion sort selection sort 26
  • 27. Sorting challenge 1 Problem. Sort a file of huge records with tiny keys. Ex. Reorganize your MP3 files. Which sorting method to use? • System sort. • Insertion sort. • Selection sort. 27
  • 28. Sorting challenge 1 Problem. Sort a file of huge records with tiny keys. Ex. Reorganize your MP3 files. Which sorting method to use? • System sort. probably no, selection sort simpler and faster • Insertion sort. no, too many exchanges • Selection sort. yes, linear time under reasonable assumptions Ex: 5,000 records, each 2 million bytes with 100-byte keys.  Cost of comparisons: 100 × 50002 / 2 = 1.25 billion.  Cost of exchanges: 2,000,000 × 5,000 = 10 trillion.  System sort might be a factor of log (5000) slower. 28
  • 29. Sorting challenge 2 Problem. Sort a huge randomly-ordered file of small records. Ex. Process transaction records for a phone company. Which sorting method to use? • System sort. • Insertion sort. • Selection sort. 29
  • 30. Sorting challenge 2 Problem. Sort a huge randomly-ordered file of small records. Ex. Process transaction records for a phone company. Which sorting method to use? • System sort. yes, it's designed for this problem • Insertion sort. no, quadratic time for randomly ordered files • Selection sort. no, always quadratic time 30
  • 31. Sorting challenge 3 Problem. Sort a huge number of tiny files (each file is independent) Ex. Daily customer transaction records. Which sorting method to use? • System sort. • Insertion sort. • Selection sort. 31
  • 32. Sorting challenge 3 Problem. Sort a huge number of tiny files (each file is independent) Ex. Daily customer transaction records. Which sorting method to use? • System sort. no, too much overhead • Insertion sort. yes, less overhead than system sort • Selection sort. yes, less overhead than system sort Ex: 4 record file.  4 N log N + 35 = 70  2N2 = 32 32
  • 33. Sorting challenge 4 Problem. Sort a huge file that is already almost in order. Ex. Resort a huge database after a few changes. Which sorting method to use? • System sort. • Insertion sort. • Selection sort. 33
  • 34. Sorting challenge 4 Problem. Sort a huge file that is already almost in order. Ex. Resort a huge database after a few changes. Which sorting method to use? • System sort. no, insertion sort simpler and faster • Insertion sort. yes, linear time for most definitions of "in order" • Selection sort. no, always takes quadratic time Ex. • A B C D E F H I J G P K L M N O Q R S T U V W X Y Z • Z A B C D E F G H I J K L M N O P Q R S T U V W X Y 34
  • 35. rules of the game ‣ selection sort ‣ insertion sort ‣ animations ‣ shellsort 35
  • 36. Insertion sort animation left of pointer is in sorted order right of pointer is untouched a[i] i 36
  • 37. Insertion sort animation Reason it is slow: excessive data movement. 37
  • 38. Insertion sort animation Reason it is slow: excessive data movement. 37
  • 39. move through the array only one place at a time. For exa Shellsort overview smallest key happens to be at the end of the array, N steps are ment where it belongs. Shellsort is a simple extension of ins by allowing exchanges of elements that are far apart, to pro Idea. Move elements more than one position at a time by h-sorting the file. that can be efficiently sorted, eventually by insertion sort. The idea is to rearrange the array to give it the pro a 3-sorted file is 3 interleaved sorted files element (starting anywhere) h=3 Such an array is said to be h-s A E L E O P M S X R T h-sorted array is h independen A E M R terleaved together. By h-sorting E O S T we can move elements in the ar L P X make it easier to h-sort for sma a procedure for any increment h=7 ends in 1 will produce a sorted M O L E E X A S P R T One way to impleme Shellsort. h-sort the M file for a decreasing S sequence of values of to use insertion sort each h, h. E P the h subsequences. Despite L R this process, we can use an ev E T input S O R T E X A M P L E cisely because the subsequenc E 7-sort M O L E E X A S P R T L h-sorting the array, we simply 3-sort A E L E O P M S X R T A the previous elements in its larger elements to the right. W An h-sorted file is h M O P R sorted files using the insertion-sort code, 1-sort A E E L interleaved S T X Shellsort trace (array contents after each pass) or decrement by h instead of 1 array. This observation reduces the shellsort implementatio 38
  • 40. h-sorting How to h-sort a file? Insertion sort, with stride length h. 3-sorting a file M O L E E X A S P R T E O L M E X A S P R T E E L M O X A S P R T E E L M O X A S P R T A E L E O X M S P R T A E L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T Why insertion sort? • Big increments ⇒ small subfiles. • Small increments ⇒ nearly in order. [stay tuned] 39
  • 41. Shellsort example input 1-sort S O R T E X A M P L E A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T 7-sort A E E L O P M S X R T S O R T E X A M 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 R T E X A S P L E A E E L M O P S X R T M O L T E X A S P R E A E E L M O P 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 A E E L M O P R S X T A E E L M O P R S T X 3-sort M O L E E X A S P R T E O L M E X A S P R T E E L M O X A S P R T result 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 L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T 40
  • 42. Shellsort: Java implementation public class Shell { public static void sort(Comparable[] a) { int N = a.length; int[] incs = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, magic increment 336, 112, 48, 21, 7, 3, 1 sequence }; for (int k = 0; k < incs.length; k++) { int h = incs[k]; for (int i = h; i < N; i++) for (int j = i; j >= h; j-= h) if (less(a[j], a[j-h])) insertion sort exch(a, j, j-h); else break; } } private boolean less(Comparable v, Comparable w) { /* as before */ } private boolean exch(Comparable[] a, int i, int j) { /* as before */ } } 41
  • 43. Visual trace of shellsort input 112-sorted 48-sorted 21-sorted 7-sorted 3-sorted result 42
  • 44. Shellsort animation big increment small increment 43
  • 45. Shellsort animation Bottom line: substantially faster than insertion sort! 44
  • 46. Shellsort animation Bottom line: substantially faster than insertion sort! 44
  • 47. Empirical analysis of shellsort Property. The number of compares used by shellsort with the increments 1, 4, 13, 40, ... is at most by a small multiple of N times the # of increments used. 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 Remark. Accurate model has not yet been discovered (!) 45
  • 48. Shellsort: mathematical analysis Proposition. A g-sorted array remains g-sorted after h-sorting it. Pf. Harder than you'd think! 7-sort 3-sort M O R T E X A S P L E M O L E E X A S P R T M O R T E X A S P L E E O L M E X A S P R T M O L T E X A S P R E E E L M O X A S P R T M O L E E X A S P R T E E L M O X A S P R T M O L E E X A S P R T A E L E O X M S P R T A E L E O X M S P R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T A E L E O P M S X R T still 7-sorted Proposition. The worst-case number of compares for shellsort using the 3x+1 increment sequence 1, 4, 13, 40, 121, 364, … is O(N3/2). 46
  • 49. 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? open problem: find a better increment sequence Lesson. Some good algorithms are still waiting discovery. 47

Notes de l'éditeur

  1. 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.
  2. details for bored safe to this.N - that.N since both are nonnegative
  3. A1: the sorting algorithm could change the entries in a[], e.g., all to the same value. Then it would surely pass the isSorted() test.
  4. assumes we&apos;re sorting real numbers
  5. two helper functions
  6. trucks parked at waiting station, leave in order of timestamp -&gt; expensive exchange, easy compare array contents just after each exchange
  7. assumes we&apos;re sorting real numbers
  8. array contents just after each insertion
  9. insertion sort (left) selection sort (right)
  10. Note: picture only intended for terminology, not to be consistent with sizes in problem
  11. ok, in reality system sort might just cutoff to insertion sort for small N
  12. red = element to move right into position, with stride length 3 Invented by D. L. Shell in 1959, hence its name
  13. A. Insertion sort is the method of choice for big and small increments!