130 likes | 333 Views
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) {
E N D
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.getName())); } // Comparing by Date public boolean equals(Object o) { Person pr = (Person) o; // VERY IMPORTANT: DOWN-CAST!!! return (born.equals(pr.getBorn())); } // Comparing by name and Date // return (name.equals(pr.getName()) & born.equals(pr.getBorn())); }
Implicit reference: String zz = “Yamen) Person p1 = new Person(“Yamen”, “M”, new Date(“30/11/2005”)); // OK Date d1 = new Date(“30/11/2005”); Person p1 = new Person(“Yamen”, “M”, d1); // 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; if(id == st.getId()) return 0; else if(id > st.getId()) return 1; else return -1; } } Student st1 = new Student(245898, “Ahmed”, 3.4); Student st2 = new Student(221573, “Ahmed”, 3.4); int res = st1.mycompare(st2); System.out.println(res); // Output: 1 int res = st2.mycompare(st1); System.out.println(res); // Output: -1 int res = st1.mycompare(st1); System.out.println(res); // Output: 0
public static void sort(int[] arr); public static void sort(double[] arr); ……… public static void sort(float[] arr); 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); The sort() method will ask the class Student whether it implements the Comparable Interface. If Yes: DO THE SORT If No: Throw an exception: ClassCastException arr sts 2 4.5 -2.7 Student 33 -55.5 17 Student public static void sort(Comparable[] arr); Student
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 compareTo(Object o) { Student st = (Student) o; if(id == st.getId()) return 0; else if(id > st.getId()) return 1; else return -1; } } Student st1 = new Student(245898, “Ahmed”, 3.4); Student st2 = new Student(221573, “Ahmed”, 3.4); int res = st1.compareTo(st2); System.out.println(res); // Output: 1 int res = st2. compareTo(st1); System.out.println(res); // Output: -1 int res = st1. compareTo(st1); System.out.println(res); // Output: 0
class ComparatorID implements Comparator { public int compare(Object o1, Object o2) { Student st1 = (Student) o1; Student st2 = (Student) o2; if(st1.getId() == st2.getId()) return 0; else if(st1.getId() > st2.getId()) return 1; else return -1; } } Interface Comparator { public int compare(Object o1, Object o2); public boolean equals(Object o); } class ComparatorNAME implements Comparator { public int compare(Object o1, Object o2) { Student st1 = (Student) o1; Student st2 = (Student) o2; String name1 = st1.getName(); String name2 = st2.getName(); if(name1.compareTo(name2) == 0) return 0; else if(name1.compareTo(name2) > 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!!! MyComparatorID cmp1 = new MyComparatorID(); double gpa = findGpa(sts, 24356, cmp1); Arrays.sort(sts, cmp1); // Sort the array based on the name!!! MyComparatorNAME cmp2 = new MyComparatorNAME(); double gpa = findGpa(sts, “Ali”, cmp2); Arrays.sort(sts, cmp2);
public static int search(Student[] arr, int idKey, Comparator cmp) { Student key = new Student(idKey, “”, 0.0); Arrays.sort(arr, cmp); int res = Arrays.binarySearch(arr, key, cmp); return res; } public static double findGpa(Student[] arr, int idKey, Comparator cmp) { int pos = search(arr, idKey, cmp); if(pos != -1) return arr[pos].getGpa(); else }
public static int search(Student[] arr, String nameKey, Comparator cmp) { Student key = new Student(0, nameKey, 0.0); Arrays.sort(arr, cmp); int res = Arrays.binarySearch(arr, key, cmp); return res; } public static double findGpa(Student[] arr, int idKey, Comparator cmp) { int pos = search(arr, idKey, cmp); if(pos != -1) return arr[pos].getGpa(); 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 n+1 cases 1+2+3….+n+n Average case: = n+1 n+1
if(cmp.compare(a[i], splitValue) <= 0) private static int split(Object[] a, int begin, int end, Comparator cmp)
Natural ordering: Numbers - Small to Large Strings - Alphabetical order: LEXICOGRAPHICAL ORDER