class Person private String name private String gender



![public static void sort(int[] arr); public static void sort(double[] arr); ……… public static void public static void sort(int[] arr); public static void sort(double[] arr); ……… public static void](https://slidetodoc.com/presentation_image/2d6e5e67448ebca8b1b8fa2dd0e11d8d/image-4.jpg)


![Student[] sts = new Student[100]; sts[0] = new Student(……); sts[1] = new Student(……); //// Student[] sts = new Student[100]; sts[0] = new Student(……); sts[1] = new Student(……); ////](https://slidetodoc.com/presentation_image/2d6e5e67448ebca8b1b8fa2dd0e11d8d/image-7.jpg)
![public static int search(Student[] arr, int id. Key, Comparator cmp) { Student key = public static int search(Student[] arr, int id. Key, Comparator cmp) { Student key =](https://slidetodoc.com/presentation_image/2d6e5e67448ebca8b1b8fa2dd0e11d8d/image-8.jpg)
![public static int search(Student[] arr, String name. Key, Comparator cmp) { Student key = public static int search(Student[] arr, String name. Key, Comparator cmp) { Student key =](https://slidetodoc.com/presentation_image/2d6e5e67448ebca8b1b8fa2dd0e11d8d/image-9.jpg)

![private static int split(Object[] a, int begin, int end, Comparator cmp) if(cmp. compare(a[i], split. private static int split(Object[] a, int begin, int end, Comparator cmp) if(cmp. compare(a[i], split.](https://slidetodoc.com/presentation_image/2d6e5e67448ebca8b1b8fa2dd0e11d8d/image-11.jpg)

- Slides: 12
class Person { private String name; private String gender; private Date born; public Person(String name, String gender, Date born) { this. name = name; this. gender = gender; this. born = born; } // Comparing by name!!! public boolean equals(Object o) { Person pr = (Person) o; // VERY IMPORTANT: DOWN-CAST!!! return (name. equals(pr. get. Name())); } // Comparing by Date public boolean equals(Object o) { Person pr = (Person) o; // VERY IMPORTANT: DOWN-CAST!!! return (born. equals(pr. get. Born())); } // Comparing by name and Date // return (name. equals(pr. get. Name()) & born. equals(pr. get. Born())); }
Implicit reference: String zz = “Yamen) Person p 1 = new Person(“Yamen”, “M”, new Date(“ 30/11/2005”)); // OK Date d 1 = new Date(“ 30/11/2005”); Person p 1 = new Person(“Yamen”, “M”, d 1); // OK
class Student { private int id; private String name; private double gpa; public Student(int id, String name, double gpa) { this. id = id; this. name = name; this. gpa = gpa; } public int mycompare(Object o) { Student st = (Student) o; Student st 1 = new Student(245898, “Ahmed”, 3. 4); if(id == st. get. Id()) Student st 2 = new Student(221573, “Ahmed”, 3. 4); return 0; int res = st 1. mycompare(st 2); else if(id > st. get. Id()) System. out. println(res); // Output: 1 return 1; int res = st 2. mycompare(st 1); else System. out. println(res); // Output: -1 return -1; int res = st 1. mycompare(st 1); } System. out. println(res); // Output: 0 }
public static void sort(int[] arr); public static void sort(double[] arr); ……… public static void sort(float[] arr); The sort() method will ask the class Student whether it implements the Comparable Interface. If Yes: DO THE SORT If No: Throw an exception: Class. Cast. Exception Student[] sts = new Student[100]; sts[0] = new Student(……); sts[1] = new Student(……); //// sts[98] = new Student(……); sts[99] = new Student(……); // Sort the array based on the id!!! Arrays. sort(arr); Arrays. sort(sts); arr sts 2 4. 5 -2. 7 Student 33 -55. 5 17 Student public static void sort(Comparable[] arr);
class Student implements Comparable { private int id; private String name; private double gpa; public Student(int id, String name, double gpa) { this. id = id; this. name = name; this. gpa = gpa; } public int compare. To(Object o) { Student st = (Student) o; Student st 1 = new Student(245898, “Ahmed”, 3. 4); if(id == st. get. Id()) Student st 2 = new Student(221573, “Ahmed”, 3. 4); return 0; int res = st 1. compare. To(st 2); else if(id > st. get. Id()) System. out. println(res); // Output: 1 return 1; int res = st 2. compare. To(st 1); else System. out. println(res); // Output: -1 return -1; int res = st 1. compare. To(st 1); } System. out. println(res); // Output: 0 }
class Comparator. ID implements Comparator { public int compare(Object o 1, Object o 2) { Student st 1 = (Student) o 1; Student st 2 = (Student) o 2; Interface Comparator { if(st 1. get. Id() == st 2. get. Id()) public int compare(Object o 1, Object o 2); return 0; public boolean equals(Object o); else if(st 1. get. Id() > st 2. get. Id()) } return 1; else return -1; } class Comparator. NAME implements Comparator { } public int compare(Object o 1, Object o 2) { Student st 1 = (Student) o 1; Student st 2 = (Student) o 2; String name 1 = st 1. get. Name(); String name 2 = st 2. get. Name(); if(name 1. compare. To(name 2) == 0) return 0; else if(name 1. compare. To(name 2) > 0) return 1; else return -1; } }
Student[] sts = new Student[100]; sts[0] = new Student(……); sts[1] = new Student(……); //// sts[98] = new Student(……); sts[99] = new Student(……); // Sort the array based on the id!!! My. Comparator. ID cmp 1 = new My. Comparator. ID(); double gpa = find. Gpa(sts, 24356, cmp 1); Arrays. sort(sts, cmp 1); // Sort the array based on the name!!! My. Comparator. NAME cmp 2 = new My. Comparator. NAME(); double gpa = find. Gpa(sts, “Ali”, cmp 2); Arrays. sort(sts, cmp 2);
public static int search(Student[] arr, int id. Key, Comparator cmp) { Student key = new Student(id. Key, “”, 0. 0); Arrays. sort(arr, cmp); int res = Arrays. binary. Search(arr, key, cmp); return res; } public static double find. Gpa(Student[] arr, int id. Key, Comparator cmp) { int pos = search(arr, id. Key, cmp); if(pos != -1) return arr[pos]. get. Gpa(); else }
public static int search(Student[] arr, String name. Key, Comparator cmp) { Student key = new Student(0, name. Key, 0. 0); Arrays. sort(arr, cmp); int res = Arrays. binary. Search(arr, key, cmp); return res; } public static double find. Gpa(Student[] arr, int id. Key, Comparator cmp) { int pos = search(arr, id. Key, cmp); if(pos != -1) return arr[pos]. get. Gpa(); else }
Array of size: n Best case: 1 comparison Key at the first element Second Best case: 2 comparisons Key at the second element …. . First worst case: n comparisons Key at the last element Second worst case: n comparisons Key is NOT found Average case: 1+2+3…. +n+n n+1 = n+1 cases
private static int split(Object[] a, int begin, int end, Comparator cmp) if(cmp. compare(a[i], split. Value) <= 0)
Natural ordering: Numbers - Small to Large Strings - Alphabetical order: LEXICOGRAPHICAL ORDER