Results 1 to 5 of 5
  1. #1
    daiviko is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default What is the best search algorithm for this problem?

    I am pretty new to Java and programming in general. I have this text file which has several million lines of text. I am trying to search every line for a particular word. If the line has the desired word then I print that line.

    I naively tried to use a while loop, and I used a loop counter just to make sure that the program is actually still searching; it's currently at around line 10,000,000. There has to be a better method for doing this.

    Any suggestions?

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: What is the best search algorithm for this problem?

    grep? I know this is a java forum, but there are better, non-java ways of doing this. Or regex. Java's regex engine can do this much better than you can (not that you're bad, just that you're almost certainly not as good as the regex engine)

  3. #3
    daiviko is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: What is the best search algorithm for this problem?

    I'm not sure what the regex engine is? Could you elaborate a bit?

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: What is the best search algorithm for this problem?

    Good site
    As is this.

    Regex takes a pattern and attempts to match it. You can set it up to retain any line that matches the pattern.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: What is the best search algorithm for this problem?

    Unless you are doing pattern matching, using a regular expression might actually slow things down. If all you are doing is looking for a word, indexOf from a String representing the line can do the job. You mention using a while loop, how do you loop over each line? Before optimizing, you should figure out what exactly is the bottleneck (is it the search for the word, the looping over the line, the reading of the File?), and showing how you are looping might give us an idea

Similar Threads

  1. A new search algorithm by me....is it good????
    By drakula941 in forum Advanced Java
    Replies: 14
    Last Post: 11-24-2011, 06:53 AM
  2. build search tree on minimax algorithm...
    By me26 in forum Java Gaming
    Replies: 2
    Last Post: 06-29-2010, 08:24 AM
  3. Algorithm Problem
    By Aggy in forum New To Java
    Replies: 4
    Last Post: 01-21-2010, 11:00 PM
  4. Replies: 0
    Last Post: 04-12-2008, 08:38 PM
  5. Problem with algorithm
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 03:31 PM

Tags for this Thread

Posting Permissions

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