Results 1 to 2 of 2
  1. #1
    Chi
    Chi is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Problem with binary search

    Hi everyone.
    Can anyone tell me how to create an array of Comparable objects to test my binary search? I need to create a driver
    Java Code:
    public class Searching public static Comparable binarySearch (Comparable[] data,
                                              Comparable target)
       {
          Comparable result = null;
          int first = 0, last = data.length-1, mid;
    
          while (result == null && first <= last)
          {
             mid = (first + last) / 2;  // determine midpoint
             if (data[mid].compareTo(target) == 0)
                result = data[mid];
             else
                if (data[mid].compareTo(target) > 0)
                   last = mid - 1;
                else
                   first = mid + 1;
          }
    
          return result;
       }
    
    }
    Last edited by Chi; 03-07-2011 at 09:06 PM.

  2. #2
    Chi
    Chi is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    To any of my friends in CSC205, to create a recursive algorithm of the binary search, you need to have a helper method that performs the recursive part. The binary search only returns what you found using the helper method.
    Last edited by Chi; 03-07-2011 at 09:07 PM.

Similar Threads

  1. Binary search problem
    By billy in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 08:43 PM
  2. Custom binary search problem
    By PtblDuffy in forum New To Java
    Replies: 2
    Last Post: 09-16-2010, 08:53 PM
  3. Binary Search Problem
    By sharpnova in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 10:22 AM
  4. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 06:19 AM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03: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
  •