+ Reply to Thread
Results 1 to 7 of 7

Thread: Binary Search

  1. #1
    Learning Programmer ReignInChaos will become famous soon enough ReignInChaos's Avatar
    Join Date
    Feb 2009
    Location
    NJ
    Age
    23
    Posts
    37
    Blog Entries
    5

    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 09:20 PM. Reason: I'm an idiot

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Binary Search

    Not bad! +rep.

  3. #3
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  4. #4
    Learning Programmer ReignInChaos will become famous soon enough ReignInChaos's Avatar
    Join Date
    Feb 2009
    Location
    NJ
    Age
    23
    Posts
    37
    Blog Entries
    5

    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.

  5. #5
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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+

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  6. #6
    Learning Programmer ReignInChaos will become famous soon enough ReignInChaos's Avatar
    Join Date
    Feb 2009
    Location
    NJ
    Age
    23
    Posts
    37
    Blog Entries
    5

    Re: Binary Search

    Sure I'll send you the whole .java file

  7. #7
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: Binary Search

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ 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. Computer Theory: Signed Integers
    By John in forum Tutorials
    Replies: 3
    Last Post: 11-30-2008, 09:00 AM
  2. Google Hacking
    By John in forum Tutorials
    Replies: 6
    Last Post: 07-22-2008, 09:37 AM
  3. Replies: 1
    Last Post: 11-23-2007, 10:09 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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