This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes recursive binary search : used code may have alternative? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursive binary search : used code may have alternative?" Watch "recursive binary search : used code may have alternative?" New topic
Author

recursive binary search : used code may have alternative?

Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
SUBJECT:recursive binary search : used code may have alternative...?

I used

you think is correct? any other solution? it works!
Lavanya Halliwell
Ranch Hand

Joined: Apr 09, 2010
Posts: 57
definitely an interesting problem, I tried this and if I start my "sorted" array at 1 I get 1 less then the key as a final result (1,2,3,4,5....). If I start it at 0 I get the key. (0,1,2,3,4,5...)

with "sorted" as {0,1,2,3,4,5,6,7,8,9} and key = 5

first: start=5 end=end

second: start=5 end=7

anyway, here is my netbeans code....




anyway, seems to work great if I start the list at 0
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
The part of code mentioned in beginning is not really required;
also mid = (first+upto)/2;

consider the foll. code:-


I also don't see the point of having two seperate functions binary search and rbinarysearch;
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
Without



if is odd or/and even the number of elements in array[i forget whichone of two], the first element does not return 0 ...however second element corrntly returns 1...
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
I didn't get you properly;

But my code works perfectly whether number of elements in array are odd or even(array is sorted in ascending order).
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
is about search NOT sort, if you key=element in 0 index, then works? I will check yours later...
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
Yes my code works even if key is element at index 0.The function returns 0 in that case;
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
Your code (lastly code scriptlet) tested by me and works, thks for the tip...Also my code considered wrong, hence still works?
Is wrong / May used
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Leonidas Savvides wrote:Your code (lastly code scriptlet) tested by me and works, thks for the tip...Also my code considered wrong, hence still works?
Is wrong / May used
Don't understand. And your use of abbreviations like thks doesn't help.
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
Your code (lastly code scriptlet) tested by me and works GOOD,
thanks for this tip...

Also my code considered wrong, since still works?
Is wrong / May used
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Just as there are many roads that lead to Rome, there are many solutions to any program. In the end, if your solution works (and is tested successfully with multiple inputs, both with and without matches) then it is a correct solution.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: recursive binary search : used code may have alternative?