Solved

Trying to find an element in a string array

Posted on 1998-08-07
9
125 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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