Computer Science Example
Searching Methods

import java.io.*;
/* This application tests out four different searching methods, two linear searches and two binary searches. */
public class SearchingMethods
{
     public static void main (String [] args)
     {
          ListClass listClass = new ListClass ();
          listClass.readList ();
          listClass.findId ();
     }
} // class SearchingMethods

// The Node class defines a node with an id and a name.  It can read and display itself.
class Node
{
    private String id, name;

     Node (String i) {id = i;}

     protected String getId () {return id;}

     public void readNode (BufferedReader infile, String i) throws IOException
     {
         id = i;
          name = infile.readLine ();
     } // method readNode

     public void displayNode () {System.out.println ("ID: " + id + " Name: " + name);}
} // class Node

/* The ListClass defines and reads in an array of Nodes.  It has four methods for finding a Node in the array. */
class ListClass
{
     final int maxSize = 100;
     private Node [] list = new Node [maxSize];
     private int listSize = 0;
 
     // Method that reads data from a file and stores it in an array of Nodes.
    public void readList ()
     {
         try
          {
               BufferedReader infile = new BufferedReader  (new InputStreamReader (new FileInputStream ("infile.txt")));
               String id = infile.readLine ();
               while (id != null && listSize < maxSize)
               {
                        Node newNode = new Node (id);
                        newNode.readNode (infile, id);
                        list [listSize] = newNode;
                        listSize ++;
                        id = infile.readLine ();
               }
               infile.close ();
          }catch (IOException e) {}
     } // method readList

    /* Method that prompts for and then reads in an id.  It then calls one of four searching methods to find the corresponding node. */
     public void findId ()
     {
          try
          {
               BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
               System.out.print ("Enter ID to locate: ");
               String keyId = stdin.readLine ();
               Node foundNode = searchList4 (keyId);
               if (foundNode != null)
                    foundNode.displayNode ();
               else
                    System.out.println ("ID not found");
          }catch (IOException e) {}
     } // method findId
 
     // Method that uses a boolean to help with the search.
     public Node searchList1 (String keyId)
     {
          int location = 0;
          boolean found = false;
 
          while (!found && (location < listSize))
               if (list [location].getId ().equals (keyId)) found = true;
               else location ++;
          if (found) return list [location];
          else return null;
     } // method searchList1
 
     // Method that uses ‘lazy evaluation’ in the while loop condition when searching for a Node.
     public Node searchList2 (String keyId)
     {
          int location = 0;
 
          while ((location < listSize) && !(list[location].getId ().equals (keyId)))
               location ++;
          return list[location];
     }//method searchList2
 
     // Non-recursive binary search.
    public Node searchList3 (String keyId)
     {
          int low = 0, high = listSize-1, mid = 0;
          boolean found = false;

          while ((low <= high) && (found == false))
          {
               mid = (low + high) / 2;
               if (list [mid].getId ().equals (keyId)) found = true;
               else
               {
                    if (list [mid].getId ().compareTo (keyId) < 0) low = mid + 1;
                    else  high = mid - 1;
               }
          }
          if (found == true) return list[mid];
          else return null;
     } // method searchList3
 
     // public method that calls a recursive version of the binary search.
     public Node searchList4 (String keyId)
     {
          return binarySearch (0, listSize-1, keyId);
     } // method searchList4
 
     // Recursive binary Search.
     private Node binarySearch (int low, int high, String keyId)
     {
          int mid ;
          if (low > high) return null;
          else
          {
               mid = (low + high) / 2;
               if (list [mid].getId ().equals (keyId)) return list[mid];
               else
                    if (list [mid].getId ().compareTo (keyId) > 0)
                         return binarySearch (low, mid-1, keyId);
                    else
                         return binarySearch (mid+1, high, keyId);
          }
     } // method binarySearch
} // class ListClass