Exemple #1
0
 public Comparable lsearch(Comparable item) {
   sort();
   for (int i = 0; i < a.length; i++) {
     if (a[i].compareTo(item) == 0) {
       return a[i];
     }
   }
   return null;
 }
Exemple #2
0
 public static void main(String[] args) {
   Random rnd = new Random();
   Search s = new Search(10);
   for (int i = 0; i < 10; i++) {
     s.addTerm(i, rnd.nextInt(15));
   }
   s.sort();
   System.out.println(s);
   System.out.println(s.rbsearch(5));
 }
Exemple #3
0
 public Comparable bsearch(Comparable item) {
   sort();
   int high = a.length;
   int low = 0;
   while ((high - low) > 0) {
     int mid = (high + low) / 2;
     int compare = a[mid].compareTo(item);
     if (compare == 0) {
       return a[mid];
     } else if (compare > 0) {
       high = mid - 1;
     } else {
       low = mid + 1;
     }
   }
   return null;
 }
Exemple #4
0
 public Comparable rbsearch(Comparable item) {
   sort();
   return rbsearch(item, a.length, 0);
 }