Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default 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);

    }
    }
    Am I on the right track?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    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

  1. Binary Search Tree Traversal
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 11-07-2008, 01:01 AM
  2. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 07:19 AM
  3. Binary Search in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  4. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 08:13 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •