Quickest way to find the string in 1 dimensional string array!

  • Thread starter Thread starter Jozef Jarosciak
  • Start date Start date
J

Jozef Jarosciak

Quickest way to find the string in 1 dimensional string array!

I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?


Dim queue(1000000) As String
Dim lookfor as String
Dim count, isthere as Integer

While queue(count) <> Nothing
If queue(count) = lookfor Then
isthere = 1
End If
listing2 = count + 1
End While

If isthere = 1 then messagebox.show("Found it!")
else messagebox.show("Didn't Find it!")
End if


Please let me know.
Joe
 
Jozef Jarosciak said:
Quickest way to find the string in 1 dimensional string array!

\\\
Dim Items() As String = ...
Dim Pattern As String = ...
Dim Found As Boolean
For Each Item As String In Items
If Item = Pattern Then
Found = True
Exit For
End If
Next Item
MsgBox(Found)
///

The code above is a linear search, which will check items until the item is
found. Its runtime is in O(n) for n items. A faster search algorithm is
Binary Search, which requires sorted data. If your data is already sorted
or you plan to perform multiple queries for the same database, check out the
array's 'Sort' and 'BinarySearch' method. The runtime of 'Sort' is Theta(n
log(n)) for n items, and 'BinarySearch' can be performed in O(log n) for n
items.
 
Jozef,

In addition to Herfried's response, if the name of your array suggests its
use, there is a Queue class that you might find useful.

Kerry Moorman
 
Herfried,
thanks a lot for the code. I am going to use it. Thanks once again for
your time.
This was very helpful.
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top