Results 1 to 3 of 3
Thread: Recursive Binary Search
- 11-20-2008, 09:00 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
Recursive Binary Search
I have to do a recursive binary search, and this is what I have:
// recursive version of binarySearch
public static Comparable binarySearchRecursive(int first, int last,
Comparable[] data,
Comparable target)
{
if (first < last)
{
int mid = first + (last - first) / 2;
if (data[mid].compareTo(target) < 0)
{
return binarySearchRecursive(mid + 1, last, data, target);
}
else if (data[mid].compareTo(target) > 0)
{
return binarySearchRecursive(mid - 1, last, data, target);
}
else
{
return mid;
}
}
return -(first + 1);
}
}
- 11-21-2008, 06:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Did you test your code? What happen when you compile and run? Any errors? You should test your application first of all, then ask any questions/doubts here we'll help you to find the solution.
- 11-21-2008, 07:26 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
Yea it gave me a few errors, but the error didn't have to do with this code. It had to do with the code my instructor gave me to compile and with the help of this code when completed. idk if I got it to work properly or not, hopefully I get some points on it haha, thanks anyways
Last edited by EternalSolitude; 11-21-2008 at 07:31 AM.
Similar Threads
-
Binary Search Tree Traversal
By dch414 in forum New To JavaReplies: 2Last Post: 11-07-2008, 01:01 AM -
Help. Binary Search Problem
By Krooger in forum Advanced JavaReplies: 1Last Post: 11-03-2008, 07:19 AM -
Binary Search in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 08:43 PM -
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01-14-2008, 08:13 PM -
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08-02-2007, 04:08 PM
Bookmarks