| Author |
recursive binary search : used code may have alternative?
|
Leonidas Savvides
Ranch Hand
Joined: Jan 31, 2010
Posts: 397
|
|
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: 397
|
|
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: 397
|
|
|
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: 397
|
|
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: 32708
|
|
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: 397
|
|
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: 19216
|
|
|
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
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: recursive binary search : used code may have alternative?
|
|
|