java - How to output binary search method? -


this average binary search, in program use search method allow user book titles.

string [] binaryarray = {"4", "6", "10"}; //the chosen binary numbers  public static boolean binarysearch(string [] a, int left, int right, string v) {     int middle;     boolean found = false;      while (found == false && left <= right) {         //if middle item == 0, returns true         middle = (left + right)/2;         int compare = a[middle].compareto(v);         if (compare == 0) {             found = true;         } else {             if (compare >0) {                 right = middle -1;             } else {                 left = middle + 1;             }         }     }     if (left > right) {         return false;     } else {         return true;     } }  

now choose if statements output correct response, imagine if database real library possibly millions of entries. not work well. way way can find response needed without if statements? ideas or advice appreciated!

private void findbookactionperformed(java.awt.event.actionevent evt) { //finds book...                                             string input = enternumberfield.gettext(); //input number see if matches      if ("4".equals(input)) {         binarysearchfield.settext("book title" + binarysearch(binaryarray, 0, binaryarray.length-1, "4"));     } else if("6".equals(input)){         binarysearchfield.settext("book title" + binarysearch(binaryarray, 0, binaryarray.length-1, "6"));     } else if("10".equals(input)){         binarysearchfield.settext("book title" + binarysearch(binaryarray, 0, binaryarray.length-1, "10"));     } } 


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -