Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to find an element in a string array

Posted on 1998-08-07
9
Medium Priority
?
128 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 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