programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• paul wheaton
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Ganesh Patekar
Bartenders:
• Tim Holloway
• Carey Brown
• salvin francis

# recursive binary search : used code may have alternative?

Ranch Hand
Posts: 403
SUBJECT:recursive binary search : used code may have alternative...?

I used

you think is correct? any other solution? it works!

Ranch Hand
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

Greenhorn
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
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
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
Posts: 403
is about search NOT sort, if you key=element in 0 index, then works? I will check yours later...

Vineet Kakati
Greenhorn
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
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

Marshal
Posts: 65457
248

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
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

Sheriff
Posts: 21788
104
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.

 That feels good. Thanks. Here's a tiny ad: Enterprise-grade Excel API for Java https://products.aspose.com/cells/java