Title: class Person
1class 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.getNam
e()) born.equals(pr.getBorn()))
2Implicit 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
3class 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 gt 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.pri
ntln(res) // Output 1 int res
st2.mycompare(st1) System.out.println(res) //
Output -1 int res st1.mycompare(st1) System.ou
t.println(res) // Output 0
4public static void sort(int arr) public static
void sort(double arr) public static void
sort(float arr)
Student sts new Student100 sts0 new
Student() sts1 new Student() //// //// s
ts98 new Student() sts99 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
5class 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 gt 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.pri
ntln(res) // Output 1 int res st2.
compareTo(st1) System.out.println(res) //
Output -1 int res st1. compareTo(st1) System.o
ut.println(res) // Output 0
6class 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() gt 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) gt 0)
return 1 else return -1
7Student sts new Student100 sts0 new
Student() sts1 new Student() //// //// s
ts98 new Student() sts99 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)
8public 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
arrpos.getGpa() else
9public 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 arrpos.getGpa() else
10Array 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
n1 cases
123.nn
Average case
n1
n1
11if(cmp.compare(ai, splitValue) lt 0)
- private static int split(Object a, int begin,
int end, Comparator cmp)
12Natural ordering Numbers -? Small to Large
Strings -? Alphabetical order LEXICOGRAPHICAL
ORDER