Solved

Trying to find an element in a string array

Posted on 1998-08-07
9
124 Views
Last Modified: 2010-04-04
Hi...
  I need to be able to search for a string in a string array of 250 different constants. Basically I read a line from a file,  then go thru a for loop 250 times checking against each element of the array to see if the line matches one of the strings in there. Then I read in the next line from the file and do the whole process again. This takes a really long time for it to run. Is there a quicker, more efficient way?

thanks,
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
9 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1361686
Hi,

I am happy that somebody up there(my local time is 9:28 p.m). And the answer is YES. We can make the complexity of your search in the order of O(log2(N)) with binary search as follows:
- Insert all your 250 strings to a TStringList and set sorted property to true
- Read aline from file and use( say you have StList)
  if StList.Find(MyCnstReadFromFile, Index) then
  begin
     // you have found it at Index'th item in the list
  end;

Regards, (need full source, just give me couple of examples about your constants but it may say several minutes)
Igor
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361687
inter,

does TStringlist uses smart search?

Zif.
0
 
LVL 5

Expert Comment

by:inter
ID: 1361688
Let me see...here in few moments...(I suppose binarry search is embeded)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:inter
ID: 1361689
Yes, It sorts with quicksort and searches with binary search. It is fast, so lets calculate,

if the set has 250 elements one should search all in normal search which has
250 ops
binary search does it in
log2(250) = log10(250)/log10(2)=7.97 => 8 ops
so there is about 250/8 = 31.25 speed up.
So in plain if it does in 100 secs
in fast mode it does it in 3.2 secs (theoretically)
Regards,
Igor
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361690
well Igor, you did it again.
0
 

Author Comment

by:aztec
ID: 1361691
Indeed, as Zifnab put it Igor, you have done it again! Works great and very fast too! Give yourself an 'A' on this one.

Thanks
   Shawn Halfpenny
0
 

Expert Comment

by:DPedrelli
ID: 1361692
If your array is sorted then you could do a Btrieve style search.  Your value is either < or > the value of the current element.  Temp[0] < Value then jump half way.  If Temp[125] > Value then jump back half way.  If Temp[75] < Value ... and so on.
If you don't require an array, you could use a TStringList which gives you two easy methods.  TStringList.IndexOf() or TStringList.Find().

D.
0
 
LVL 5

Expert Comment

by:inter
ID: 1361693
Hi, thanks,
Am I supposed to post an answer?
0
 
LVL 5

Accepted Solution

by:
inter earned 50 total points
ID: 1361694
Hi,
Just scanning all the questions and found this, he he ;-)
Regards, Igor
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question