Searching and Sorting Using Standard Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Searching and Sorting Using Standard Classes

Description:

So far, all the algorithms we have studied have been on array of integers. ... method object other of type Object is downcast to object s of type Student so ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 10
Provided by: makbarb
Category:

less

Transcript and Presenter's Notes

Title: Searching and Sorting Using Standard Classes


1
Searching and Sorting Using Standard Classes
  • Searching data other than primitive type.
  • Comparable interface.
  • Implementing compareTo() method.
  • java.util package java.util.Arrays class.
  • Programming examples
  • Preview Basic Data Structures (Part I).

2
Searching and Sorting Real Data
  • So far, all the algorithms we have studied have
    been on array of integers.
  • However, in real life, there is rarely a need to
    search or sort a collection of integers. What
    are often encountered are records of say
    students, bank accounts, books, etc., which have
    many fields.
  • In java, these records are implemented as
    objects. Thus, the question is, can we improve
    these algorithms to handle array of objects?
  • Fortunately Java has made it very easy to do
    this. All that is required is for a class to
    implement the Comparable interface which has one
    method named, compareTo(). Consider the
    following example

3
Comparable Interface compareTo() Method
  • java.lang package defines a Comparable interface
    as follows
  • public interface Comparable int compareTo
    (Object other)
  • Any class that implements the Comparable
    interface must supply the compareTo method
  • The compareTo method is intended to sort objects.
    A sorting algorithm will repeatedly call the
    compareTo method e.g.
  • first.compareTo(second)
  • depending on the return value (e.g. 1, 0, 1)
    from compareTo method, the sorting algorithm
    decides whether the objects are in correct order
    or not (sorting) OR the particular object is
    found or not (searching).

4
Example Implementing of compareTo() method
  • Suppose we have a class Student and we want to
    search record of a particular student. The
    implementation of compareTo() method is as
    follows
  • In compareTo() method object other of type Object
    is downcast to object s of type Student so that
    id of student to be search can be compare with id
    of other student. If search is successful the two
    ids matches and method returns 0.

public int compareTo(Object other)
Student s (Student) other if
(iDNumber lt s.iDNumber) return -1
else if (iDNumber gt s.iDNumber)
return 1 else return 0

class Student implements Comparable
private int iDNumber private String name
private double gPA ..
5
Example Implementing compareTo() (Contd)
public class Student implements Comparable private int iDNumber private String name private double gPA public Student(int iDNumber, String name, double gPA) this.iDNumber iDNumber this.name name this.gPA gPA public Student(int iDNumber) this(iDNumber,"",0.0) public String toString() return iDNumber"\t"name"\t"gPA public int compareTo(Object other) Student s (Student) other if (iDNumber lt s.iDNumber) return -1 else if (iDNumber gt s.iDNumber) return 1 else return 0 //other methods here
6
Example Implementing compareTo() (Contd)
import java.io.public class ComparableSearch public static intbinarySearch(Comparable a, int from, int to, Comparable v) if (from gt to) return -1 int mid (from to) / 2 int diff amid.compareTo(v) if (diff 0) // amid v return mid else if (diff lt 0) // amid lt v return binarySearch(a, mid 1, to, v) else return binarySearch(a, from, mid - 1, v) public static int search(Comparable a, Comparable v) return binarySearch(a, 0, a.length - 1, v) public static void main(String args) throws IOException BufferedReader console new BufferedReader(new InputStreamReader(System.in)) Student s new Student4 s0 new Student(955000, "Ibrahim", 3.5) s1 new Student(966000, "Amir", 2.0) s2 new Student(977000, "Talal", 2.4) s3 new Student(988000, "Usman", 2.5) for (int i 0 i lt s.length i) System.out.println(si) System.out.print("\nEnter ID Number to search for") Comparable v new Student(Integer.parseInt(console.readLine())) int j search(s, v) if (j ! -1) System.out.println("Found in position " (j1)) else System.out.println(v " not found")
7
Standard Sorting Searching Methods
  • The Arrays class of the java.util package
    implements binarySearch and a sorting method for
    both numbers and comparable objects.
  • If you wish, you can call these methods rather
    than your own implementations.
  • The following example shows how these methods may
    be used.

import java.io.import java.util.Arrayspublic class StandardSearch public static void main(String args) throws IOException BufferedReader console new BufferedReader(new InputStreamReader(System.in)) int a ArrayUtil.randomIntArray(20, 100) ArrayUtil.print(a) Arrays.sort(a) ArrayUtil.print(a) System.out.print("\nEnter n number to search for") int j Arrays.binarySearch(a, Integer.parseInt(console.readLine())) if (j ! -1) System.out.println("Found in position " (j1)) else System.out.println("element not found")
8
Standard Sorting Searching Methods (Contd)
Student s new Student4 s0 new Student(977000, "Talal", 2.4) s1 new Student(988000, "Usman", 2.5) s2 new Student(999000, "Umar", 3.0) s3 new Student(955000, "Ibrahim", 3.5) Arrays.sort(s) for (int i 0 i lt s.length i) System.out.println(si) System.out.print("\nEnter ID Number to search for") Comparable v new Student(Integer.parseInt(console.readLine())) j Arrays.binarySearch(s, v) if (j ! -1) System.out.println("Found in position " (j1)) else System.out.println(v " not found")
9
Modified ArrayUtil Class
import java.util.Randompublic class ArrayUtil public static int randomIntArray(int length, int n) int a new intlength Random generator new Random() for (int i 0 i lt a.length i) ai generator.nextInt(n) return a public static void swap(int a, int i, int j) int temp ai ai aj aj temp public static void swap(Comparable a, int i, int j) Comparable temp ai ai aj aj temp public static void print(int a) for (int i 0 i lt a.length i) System.out.print(ai " ") System.out.println() public static Comparable copyArray(Comparable a, int size) String b new Stringsize if (size lt a.length) for (int i0 iltsize i) bi (String) ai return b
Write a Comment
User Comments (0)
About PowerShow.com