===================================
This is the binary search

public static int binarySearch(int[] list, int 
listLength, int searchItem) {
    int first=0;
    int last = listLength - 1;
    int mid;

    boolean found = false;

    //Loop until found or end of list.
    while(first <= last &&!found) {
        //Find the middle.
        mid = (first + last) /2;

        //Compare the middle item to the search item.
        if(list[mid] == searchItem) found = true;
        else { //Not found, readjust search parameters, 
halving the size & start over.
            if(list[mid] > searchItem) last = mid -1;
            else first = mid + 1;
        }
    }

    if(found) return mid;
    else return(-1);
}

i want to apply that binary serch inside this program to serach it by the book title or author

// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C>java libmainsys
///////////////////////////////////////////////////////
/////////
class libary
  {
  private String booktitle;
  private String bookauthor;
  private String publisher;
  private int yearpublished;
  private int edition;
  private int nofcop;


//-----------------------------------------------------
------
  public libary(String title, String author, String 
pub, int yrpub, int edt, int nfcp)
   {                // constructor
   booktitle = title;
   bookauthor = author;
   publisher = pub; 
   yearpublished = yrpub;
   edition = edt;
   nofcop = nfcp;
   }

//-----------------------------------------------------
------
  public void displaylibary()
   {
   System.out.print("  Book Title: " + booktitle);
   System.out.print(", Book Author: " + bookauthor);
   System.out.print(", Book Publisher: " + publisher);
   System.out.print(", Year Published: " + 
yearpublished);
System.out.print(", Edition: " + edition);

   System.out.println(",No Of Copies : " + nofcop);
   }

//-----------------------------------------------------
------
  public String getLast()      // get title
   { return booktitle; }
  } // end class libary
///////////////////////////////////////////////////////
/////////
class ArrayInOb
  {
  private libary[] nfcp;        // ref to array ypub
  private int nElems;        // number of data items
//-----------------------------------------------------
---------
  public ArrayInOb(int max)     // constructor
   {
   nfcp = new libary[max];        // create the array
   nElems = 0;            // no items yet
   }
//-----------------------------------------------------
---------
                   // put libary into array
  public void insert(String title, String author, 
String pub, int yrpub, int edt, int nofcop)
   {
   nfcp[nElems] = new libary(title, author, pub, yrpub, 
edt, nofcop);
   nElems++;             // increment size
   }

//-----------------------------------------------------
---------
  public void display()       // displays array 
contents
   {
   for(int j=0; j<nElems; j++)    // for each element,
     nfcp[j].displaylibary();     // display it
   System.out.println("");
   }
//-----------------------------------------------------
---------
  public void bubbleSort()
{
  int i, j;
  libary temp;
  for (i = nElems-1; i >= 0; i--)
    for (j = 1; j <= i; j++)
      if 
(nfcp[j-1].getLast().compareTo(nfcp[j].getLast())>0)
      {
        temp = nfcp[j-1];
        nfcp[j-1] = nfcp[j];
        nfcp[j] = temp;
      }
}
//-----------------------------------------------------
---------
  } // end class ArrayInOb
///////////////////////////////////////////////////////
/////////
class libmainsys
  {
  public static void main(String[] args)
   {
   int maxSize = 1000;       // array size
   ArrayInOb arr;         // reference to array
   arr = new ArrayInOb(maxSize); // create the array
   arr.insert("Java_how__to_program", "Patty_John", 
"Deitel", 2001, 1, 430);
   arr.insert("System_Design", "Dexter_Smith", 
"Thomson", 2002, 3, 450);
   arr.insert("Program_Design", "Lorraine_Paul", 
"About", 1996, 2, 196);
   arr.insert("Computer_Architecture", 
"Paul_Andrew","Dzone", 2006, 5, 199);
   arr.insert("Visual_Basic_How_To_ Program", 
"Tom_Jones", "Jeffereson_publication",  2007, 4, 207);
   arr.insert("Information_ Management", 
"William_Peter", "Mcgraw_Hill", 1995, 3, 204);
   arr.insert("Sofware_ Application", "Henry_Sam", 
"Pearson", 2001, 6, 278);
   arr.insert("English", "Samantha_Julia", 
"James_Hill", 2005, 1, 200);
   arr.insert("Web_Publishing", "Audrey_Cynthia", 
"Surg", 2004, 3, 201);
   arr.insert("Human_Computer_Interaction", 
"Tony_Edward",  "Telde", 2003, 3, 199);
   System.out.println("Before sorting:");
   arr.display();         // display items
   arr.bubbleSort();      // insertion-sort them
   System.out.println("After sorting:");
   arr.display();         // display them again
   } // end main()

} // end class libmainsys
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.