
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.1 ELEMENTARY SORTS 2.1 ELEMENTARY SORTS ‣ rules of the game ‣ rules of the game ‣ selection sort ‣ selection sort Algorithms ‣ insertion sort Algorithms ‣ insertion sort FOURTH EDITION ‣ shellsort ‣ shellsort ‣ shuffling ‣ shuffling ROBERT SEDGEWICK | KEVIN WAYNE ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu ‣ convex hull http://algs4.cs.princeton.edu ‣ convex hull Sorting problem Inversions (a.k.a. Yodaness) Ex. Twitter Dossier. Def. An inversion is a pair of keys in an array that are out of order. Name YOB Tweets Followers Most Recent Tweet Containing A E E L M O T R X P S Justin Bieber 1994 23,532 44,865,050 u saved my life ... @justinbieber Kevin Shields 1963 21 6,165 Primal Scream ( & Kevin Shields) Kevin Barnes 1974 1,211 43,470 kevin barnes drank too much cough syrup T-R T-P T-S R-P X-P X-S item Kevin Mitnick 1963 9,380 103,952 Kevin Mitnick gives solution for NSA spying (6 inversions out of 55 max) Lil B 1989 115,746 678,610 GUYS LIL B COMMENTED ON MY PICTURE BYE Josh Hug 1981 48 30 I need a josh hug Goal. key Carly Rae Jepsen 1981 5,040 8,874,941 carly rae jepsen stop trying already ・Given an array with N inversions. ・Perform a sequence of operations that reduces inversions to 0. Sort. Rearrange array of N items into ascending order. Name YOB Tweets Followers Most Recent Tweet Containing Carly Rae Jepsen 1981 5,040 8,874,941 carly rae jepsen stop trying already Josh Hug 1981 48 30 I need a josh hug Justin Bieber 1994 23,532 44,865,050 u saved my life ... @justinbieber Kevin Barnes 1974 1,211 43,470 kevin barnes drank too much cough syrup Kevin Mitnick 1963 9,380 103,952 Kevin Mitnick gives solution for NSA spying Kevin Shields 1963 21 6,165 Primal Scream ( & Kevin Shields) Lil B 1989 115,746 678,610 GUYS LIL B COMMENTED ON MY PICTURE BYE Introduction à l'analyse des lignes courbes algébriques, Gabriel Cramer, 1750 3 4 Sample sort client 1 Sample sort client 2 Goal. Sort any type of data. Goal. Sort any type of data. Ex 1. Sort random real numbers in ascending order. Ex 2. Sort strings from file in alphabetical order. seems artificial, but stay tuned for an application public class StringSorter public class Experiment % java Experiment 10 { { 0.08614716385210452 public static void main(String[] args) public static void main(String[] args) 0.09054270895414829 { { 0.10708746304898642 String[] a = In.readStrings(args[0]); int N = Integer.parseInt(args[0]); 0.21166190071646818 Insertion.sort(a); Double[] a = new Double[N]; 0.363292849257276 for (int i = 0; i < a.length; i++) for (int i = 0; i < N; i++) 0.460954145685913 StdOut.println(a[i]); a[i] = StdRandom.uniform(); 0.5340026311350087 } Insertion.sort(a); 0.7216129793703496 } for (int i = 0; i < N; i++) 0.9003500354411443 % more words3.txt StdOut.println(a[i]); 0.9293994908845686 it’s friday, friday gotta get down on friday } } % java StringSorter words3.txt down friday friday friday, get gotta it’s on 5 6 Sample sort client 3 Callbacks Goal. Sort any type of data. Goal. Sort any type of data. Ex 3. Sort the files in a given directory by filename. Q. How can sort() know how to compare data of type Double, String, and import java.io.File; % java FileSorter . java.io.File without any information about the type of an item's key? public class FileSorter Insertion.class { Insertion.java public static void main(String[] args) InsertionX.class Callback = reference to executable code. { InsertionX.java Client passes array of objects to sort() function. File directory = new File(args[0]); Selection.class ・ File[] files = directory.listFiles(); Selection.java ・The sort() function calls back object's compareTo() method as needed. Insertion.sort(files); Shell.class for (int i = 0; i < files.length; i++) Shell.java StdOut.println(files[i].getName()); ShellX.class Implementing callbacks. } ShellX.java ・Java: interfaces. } public class File { private String path; private int prefixLength; ... } 7 8 Callbacks: roadmap Callbacks: roadmap client object implementation client object implementation import java.io.File; public class File import java.io.File; public class File public class FileSorter implements Comparable<File> public class FileSorter implements Comparable<File> { { { { public static void main(String[] args) ... public static void main(String[] args) ... { public int compareTo(File b) { public int compareTo(File b) File directory = new File(args[0]); { File directory = new File(args[0]); { File[] files = directory.listFiles(); ... File[] files = directory.listFiles(); ... Insertion.sort(files); return -1; Insertion.sort(files); return -1; for (int i = 0; i < files.length; i++) ... for (int i = 0; i < files.length; i++) ... StdOut.println(files[i].getName()); return +1; StdOut.println(files[i].getName()); return +1; } ... } ... } return 0; } return 0; } } } pollEv.com/jhug text to 37607 } Q: If we omit “compareTo()”, which Comparable interface (built in to Java) Insertion sort implementation Insertion sort implementation file will fail to compile? public static void sort(Comparable[] a) public static void sort(Comparable[] a) public interface Comparable<Item> { { { int N = a.length; int N = a.length; public int compareTo(Item that); for (int i = 0; i < N; i++) for (int i = 0; i < N; i++) } for (int j = i; j > 0; j--) A. FileSorter.java [778751] for (int j = i; j > 0; j--) if (a[j].compareTo(a[j-1]) < 0) B. File.java [778752] if (a[j].compareTo(a[j-1]) < 0) exch(a, j, j-1); C. InsertionSort.java [778753] exch(a, j, j-1); key point: no dependence else break; else break; } } on File data type 9 10 Callbacks: roadmap Comparable API client object implementation Implement compareTo() so that v.compareTo(w) import java.io.File; public class File public class FileSorter implements Comparable<File> ・Is a total order. { { Returns a negative integer, zero, or positive integer public static void main(String[] args) ... ・ { public int compareTo(File b) if v is less than, equal to, or greater than w, respectively. File directory = new File(args[0]); { File[] files = directory.listFiles(); ... Throws an exception if incompatible types (or either is null). Insertion.sort(files); return -1; ・ for (int i = 0; i < files.length; i++) ... StdOut.println(files[i].getName()); return +1; } ... } return 0; } pollEv.com/jhug text to 37607 } Q: If we omit “implements Insertion sort implementation Comparable”, which file will fail to public static void sort(Comparable[] a) compile? { int N = a.length; for (int i = 0; i < N; i++) A. FileSorter.java [778757] for (int j = i; j > 0; j--) if (a[j].compareTo(a[j-1]) < 0) B. File.java [778758] exch(a, j, j-1); C. InsertionSort.java [778759] else break; } 11 12 Total order Comparable API A total order is a binary relation ≤ that satisfies: Implement compareTo() so that v.compareTo(w) ・Antisymmetry: if v ≤ w and w ≤ v, then v = w. ・Is a total order. ・Transitivity: if v ≤ w and w ≤ x, then v ≤ x. ・Returns a negative integer, zero, or positive integer ・Totality: either v ≤ w or w ≤ v or both. if v is less than, equal to, or greater than w, respectively. ・Throws an exception if incompatible types (or either is null). Ex. ・Standard order for natural and real numbers. Chronological order for dates or times. v w ・ v w ・Alphabetical order for strings. w v … ・ less than (return -1) equal to (return 0) greater than (return +1) violates totality: (Double.NaN <= Double.NaN) is false an intransitive relation Built-in comparable types. Integer, Double, String, Date, File, ... Surprising but true. The <= operator for double is not a total order. (!) User-defined comparable types. Implement the Comparable interface. 13 14 Implementing the Comparable interface Callbacks Date data type. Simplified version of java.util.Date. Goal. Sort any type of data. public class Date implements Comparable<Date> Q. How can sort() know how to compare data of type Double, String, and { private final int month, day, year; java.io.File without any information about the type of an item's key? public Date(int m, int d, int y) only compare dates { to other dates Callback = reference to executable code. month = m; day = d; ・Client passes array of objects to sort() function. year = y; The sort() function calls back object's compareTo() method as needed. } ・ public int compareTo(Date that) Implementing callbacks. { if (this.year < that.year ) return -1; Java: interfaces. if (this.year > that.year ) return +1; ・ C: function pointers. if (this.month < that.month) return -1; ・ if (this.month > that.month) return +1; C++: class-type functors. if (this.day < that.day ) return -1; ・ if (this.day > that.day ) return +1; ・C#: delegates. return 0; Python, Perl, ML, Javascript: first-class functions. } ・ } 15 16 Summary. Two useful sorting abstractions Generic Sorting. Helper functions. Refer to data through compares and exchanges. ・Generic sorting algorithm expects array of Comparables ・Comparable: Class implements .compareTo() method Less. Is item v less than w ? – Must contain compareTo() method to compile – compareTo() should obey certain rules to guarantee function private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } Today’s Sorting Algorithms. ・Will only interact with the Comparable array via helper functions – exch(i ,j): swaps items at position i and j Exchange. Swap item in array a[] at index i with the one at index j. – less(v, w): returns true if v.compareTo(w) < 0 ・Why exchange? private static void exch(Comparable[] a, int i, int j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } 17 18 Testing Goal. 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; 2.1 ELEMENTARY SORTS return true; } ‣ rules of the game ‣ selection sort a[] TroubleSort.sort(a) Algorithms ‣ insertion sort a[] isSorted(a) true ‣ shellsort ‣ shuffling pollEv.com/jhug text to 37607 ROBERT SEDGEWICK | KEVIN WAYNE \ http://algs4.cs.princeton.edu ‣ convex hull Q.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages23 Page
-
File Size-