Array search algorithm

W

William Morris

I have a one-dimesional array of potentially thousands of items, each of
which is a string of at least one up to 30 characters, mixed letters and
numbers. I have another array - potentially as large - whose items I wish
to find in the first array.

A nested loop would answer this problem - and does for the time being.

for x = 0 to ubound(secondarray)
for y = 0 to ubound(firstarray)
if secondarray(x) = firstarray(y) then
itemExists = true
exit function
end if
next
next

I thought it would be better to loop the array and pass each element to a
recursive function that divides and conquers, BUT it misses about half of
the matches - every item I'm searching for already exists in the list, just
as a test. Below is the code I'm using...can anyone figure out what I'm
missing?

Thanks!

- Wm


Function ValueExists(inArray, idToCheck, lowerBound, upperBound)
If lowerBound = upperBound Then
If idToCheck = inArray(lowerBound) Then
ValueExists = True
End If
Exit Function
End If
Dim sPos
sPos = ((upperBound - lowerBound) \ 2) + lowerBound
idToCheck = Trim(idToCheck)
inArray(sPos) = Trim(inArray(sPos))
If idToCheck = inArray(sPos) Then
ValueExists = True
Exit Function
End If
If idToCheck < inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, lowerBound, sPos - 1)
Exit Function
End If
If idToCheck > inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, sPos + 1, upperBound)
Exit Function
End If
End Function
 
B

Bob Barrows

William said:
I have a one-dimesional array of potentially thousands of items, each
of which is a string of at least one up to 30 characters, mixed
letters and numbers. I have another array - potentially as large -
whose items I wish to find in the first array.

A nested loop would answer this problem - and does for the time being.

for x = 0 to ubound(secondarray)
for y = 0 to ubound(firstarray)
if secondarray(x) = firstarray(y) then
itemExists = true
exit function
end if
next
next

I thought it would be better to loop the array and pass each element
to a recursive function that divides and conquers, BUT it misses
about half of the matches - every item I'm searching for already
exists in the list, just as a test. Below is the code I'm
using...can anyone figure out what I'm missing?

Thanks!

- Wm


Function ValueExists(inArray, idToCheck, lowerBound, upperBound)
If lowerBound = upperBound Then
If idToCheck = inArray(lowerBound) Then
ValueExists = True
End If
Exit Function
End If
Dim sPos
sPos = ((upperBound - lowerBound) \ 2) + lowerBound
idToCheck = Trim(idToCheck)
inArray(sPos) = Trim(inArray(sPos))
If idToCheck = inArray(sPos) Then
ValueExists = True
Exit Function
End If
If idToCheck < inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, lowerBound,
sPos - 1) Exit Function
End If
If idToCheck > inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, sPos + 1,
upperBound) Exit Function
End If
End Function

Have you considered joining the array into a string and using InStr?

Function ValueExists(inArray, idToCheck)
dim str
str="|" & Join(inArray,"|") & "|"
if Instr(str,"|" & idToCheck & "|") > 0 then
ValueExists=true
else
ValueExists=false
end if
end function

HTH,
Bob Barrows
 
W

William Morris

Aww, geez. I got so focused where I was that I spaced off the JOIN
function. That'll do, thanks!

- Wm
 

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

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top