java - Binary Search - Trouble getting information -
i have issue. learning bout binary arrays , trying figure out. trying make user enter book number , search through array reflist , print through booklist.
problem when enter number greater 1 compare value never reaches 0.
     public boolean binarysearch(string [] a, int left, int right, string v){      int middle;      numofsearches ++;      if (left > right) {          return false;      }       middle = (left + right)/2;      int compare = v.compareto(a[middle]);      system.out.println("middle value: "+middle);      if (compare == 0) {          binaryoutput.settext("the book is: "+booklist[middle]);      }      if (compare < 0) {          return binarysearch(a, left, (middle-1), v);      } else {          return binarysearch(a, middle + 1, right, v);      }  } 
just forgotten return true.
 if (left > right) {      return false;  }  middle = (left + right)/2;  int compare = v.compareto(a[middle]);  system.out.println("middle value: "+middle);  if (compare == 0) {      binaryoutput.settext("the book is: "+booklist[middle]);      return true; // missing!  }  if (compare < 0) {      return binarysearch(a, left, (middle-1), v);  } else {      return binarysearch(a, middle + 1, right, v);  } also compareto might implemented incorrectly.
Comments
Post a Comment