Board index » delphi » Binary Searches on a StringList ?

Binary Searches on a StringList ?

Hi!
If I create a StringList of say 2000 items, and then make
MyList.Sorted := True , what is the fastest way to search the list at
runtime?  Is it possible to make a binary search and if so how?

It must be possible in Delphi, after all this is what an index search
does.

Any help appreciated.
rgds, John
--
John Matthews

 

Re:Binary Searches on a StringList ?


Since you're already sorted, just use the find method, it uses a binary
search algorithm (that's why it won't work unless the list is sorted.)

later.... mike

Quote
John Matthews <J...@johnmatthews.demon.co.uk> wrote:
>Hi!
>If I create a StringList of say 2000 items, and then make
>MyList.Sorted := True , what is the fastest way to search the list at
>runtime?  Is it possible to make a binary search and if so how?

>It must be possible in Delphi, after all this is what an index search
>does.

>Any help appreciated.
>rgds, John

Re:Binary Searches on a StringList ?


Hi,

Quote
>Is it possible to make a binary search and if so how?

Sure. If Sorted=True the Stringlist will use a built-in binary search
if you ask it for an ItemIndex.

Ciao, MM
--
Marian Maier, Gamma Soft, Hainstra?e 8, 53121 Bonn, Germany
Tel: +49 228 624013 Fax: +49 228 624031
http://www.gammasoft.de/maier
"Was schiefgehen kann geht schief"

Re:Binary Searches on a StringList ?


Hi!
Thanks your help with StringLists.

How can I make a rapid search of a TList ?  I have in mind a List of
pointers to records.  So I would want to search rapidly (without
iterating) a sorted TList.  Is this possible and how is it done ? My
program is currently DOS based and has several thousand records in
memory in a sorted collection, but I can't see how Delphi can do this.

Any guidance would be appreciated, since the online help is not very
forthcoming.

rgds, John

In article <YBFQWDAINbY2E...@johnmatthews.demon.co.uk>, John Matthews
<J...@johnmatthews.demon.co.uk> writes

Quote
>Hi!
>If I create a StringList of say 2000 items, and then make
>MyList.Sorted := True , what is the fastest way to search the list at
>runtime?  Is it possible to make a binary search and if so how?

>It must be possible in Delphi, after all this is what an index search
>does.

>Any help appreciated.
>rgds, John

--
John Matthews

Re:Binary Searches on a StringList ?


Quote
> How can I make a rapid search of a TList ?  I have in mind a List of
> pointers to records.  So I would want to search rapidly (without
> iterating) a sorted TList.  Is this possible and how is it done ? My
> program is currently DOS based and has several thousand records in
> memory in a sorted collection, but I can't see how Delphi can do this.

> Any guidance would be appreciated, since the online help is not very
> forthcoming.

It seems to me that perhaps another container-class might serve you
better.  For example, a hash-dictionary class could easily be built (or
purchased) that would allow you to locate any particular key in probably
the least amount of time that would be possible.  A tree is another
possibility; again, these classes can be had fairly easily.  And of
course, there is nothing to prevent you from using more than one class,
e.g. a TStringList and <something else> at the same time, to get to your
information.

Other Threads