recursion - Java - recursive binary search help -
the following code how i'm trying create recursive binary method..
public static int binarysearch(comparable[] objarray, comparable item) {      int lower=0;     int upper=objarray.length -1;     int = -1;     int compareresult;     boolean found = false;     while ((lower<=upper) && (!found))     {         i=(lower+upper)/2;         compareresult=item.compareto(objarray[i]);         if(compareresult<0)         {             upper=i-1;         }         else             if (compareresult>0)             {                 lower=i+1;             }             else             {                 found=true;             }       }     return compareresult;    }   i feel thought i'm not doing correctly...any suggestions?
-d
you using loop. in order method recursive, needs call until reaches breaking condition.
check out wikipedia's example.
essentially, "while" condition condition breaks recursion (i.e. stops method calling itself), , contents of current loop instead setting "upper" , "lower" parameters next recursive call.
Comments
Post a Comment