How to implement binary search


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to implement binary search

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    37

    How to implement binary search

    I have a text file containing several words and pointers, like this:

    String1 int1
    String2 int2
    String3 int3
    ......

    The String1, String2, String3......are sorted in lexicographical order.

    What I want to do is to search a word among those Strings, using binary search, and return the int value beside the matched String.

    Could anybody give me some detailed help on this question? Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    62
    do you know the details of the binary search algorithm? do you need help with that or do you need help implementing it in java?

    Kind regards,
    Noel
    Efficiency is intelligent laziness

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    First you have to implement a method which processes the content of the file.
    The Scanner class will help immensely in that process - I would ask my implementation to tokenize the text using the new line symbol as the delimiter. The tokens will be put into a collection.

    You can implement your own "search" of the String, or you can use the Scanner class's findInLine() method. If your current String contains the pattern, then use the nextInt() method to obtain the integer accompanying the String; then do next() to get the next token, and repeat.

    There are some other methods of the Scanner class which can search across the entire file, ignoring delimiters.

Similar Threads

  1. Binary Search
    By Dreamer in forum Java
    Replies: 0
    Last Post: 01-30-2006, 03:56 PM
  2. Replies: 1
    Last Post: 12-17-2005, 03:50 PM
  3. Binary Search Tree implementation
    By Liza in forum Java
    Replies: 0
    Last Post: 10-23-2001, 12:21 PM
  4. Binary Search trees
    By Tom in forum VB Classic
    Replies: 0
    Last Post: 12-02-2000, 01:24 PM
  5. Binary Search trees
    By Tom in forum Java
    Replies: 0
    Last Post: 12-02-2000, 01: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center