+ Reply to Thread
Results 1 to 7 of 7

Thread: Binary Search

  1. #1
    ReignInChaos's Avatar
    ReignInChaos is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    NJ
    Posts
    46
    Blog Entries
    5
    Rep Power
    12

    Binary Search

    This can most likely be found on every code site but here is an implementation of binary searching techniques with Strings.

    Searching and sorting are some of the biggest topics that are covered in CS classes, especially Data Structures. Sorted structures are the easiest to search on because the programmer already knows how the data is arranged. In this case, I am assuming my List of Strings are sorted from a-z. Here is the recursive code for binary search.


    Code:
       // recursive binary search
       private int binSrch (int lo, int hi, String target)
       {
            if (lo>hi)
               return -1;           // target not found
            int mid = (lo + hi) / 2;        // midpoint
            if (target.equals(values.get(mid)))
                    return mid;     // found the target at mid
            if (target.compareTo(values.get(mid)) < 0 )
                    return binSrch (lo, mid-1, target);
            return binSrch (mid+1, hi, target);
    
      }
    This is how it works. It enters the method with a low and high starting value along with a target String. If the target is not found, it returns -1 as the index, then creates the midpoint. Using the .equals() method, it compares the target and the String at the midpoint and returns the mid if found.

    Now here is the recursive part. Using the compareTo() method, it will compare target against the String at the midpoint. If target is shorter, it will call binSrch() again with the first half of the List binSrch (lo, mid-1, target);. Otherwise, it will do the same thing with the second half of the list.
    Last edited by ReignInChaos; 06-16-2009 at 07:20 PM. Reason: I'm an idiot

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Binary Search

    Not bad! +rep.

  4. #3
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Binary Search

    Nice. How would you go about writing a binary search function that works on any data type?

    I do know how to do it, but I'm curious as to how you would do it. =)

    +rep for you.

  5. #4
    ReignInChaos's Avatar
    ReignInChaos is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    NJ
    Posts
    46
    Blog Entries
    5
    Rep Power
    12

    Re: Binary Search

    well if I was using Java I would have binSrch accept an Object as a parameter instead of a String. ie private in binSrch(int lo, int hi, Object obj). I haven't done it but it should work in theory.

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Binary Search

    Quote Originally Posted by ReignInChaos View Post
    This can most likely be found on every code site but here is an implementation of binary searching techniques with Strings.

    Searching and sorting are some of the biggest topics that are covered in CS classes, especially Data Structures. Sorted structures are the easiest to search on because the programmer already knows how the data is arranged. In this case, I am assuming my List of Strings are sorted from a-z. Here is the recursive code for binary search.


    Code:
       // recursive binary search
       private int binSrch (int lo, int hi, String target)
       {
            if (lo>hi)
               return -1;           // target not found
            int mid = (lo + hi) / 2;        // midpoint
            if (target.equals(values.get(mid)))
                    return mid;     // found the target at mid
            if (target.compareTo(values.get(mid)) < 0 )
                    return binSrch (lo, mid-1, target);
            return binSrch (mid+1, hi, target);
    
      }
    This is how it works. It enters the method with a low and high starting value along with a target String. If the target is not found, it returns -1 as the index, then creates the midpoint. Using the .equals() method, it compares the target and the String at the midpoint and returns the mid if found.

    Now here is the recursive part. Using the compareTo() method, it will compare target against the String at the midpoint. If target is shorter, it will call binSrch() again with the first half of the List binSrch (lo, mid-1, target);. Otherwise, it will do the same thing with the second half of the list.
    Could you possible do for me a special version with a demo ?
    It looks really interesting and I would really know more !
    rep+

  7. #6
    ReignInChaos's Avatar
    ReignInChaos is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    NJ
    Posts
    46
    Blog Entries
    5
    Rep Power
    12

    Re: Binary Search

    Sure I'll send you the whole .java file

  8. #7
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Binary Search

    Quote Originally Posted by ReignInChaos View Post
    Sure I'll send you the whole .java file
    Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Binary Search Help
    By OwsumEmam in forum Java Help
    Replies: 6
    Last Post: 04-01-2011, 11:47 AM
  2. Binary search Algorithm
    By jerry4prince in forum Visual Basic Programming
    Replies: 0
    Last Post: 08-31-2010, 10:41 AM
  3. Replies: 2
    Last Post: 05-30-2010, 01:20 PM
  4. Java Binary Search help!
    By bleedy3 in forum Java Help
    Replies: 2
    Last Post: 02-13-2010, 10:10 AM
  5. Binary Search Tree Construction Help
    By ahmed in forum General Programming
    Replies: 7
    Last Post: 02-10-2010, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

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