473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Array search algorithm

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

--
William Morris
Semster, Seamlyne reProductions
Visit our website, http://www.seamlyne.com, for the most comfortable
historically inspired clothing you can buy!
Jul 19 '05 #1
2 1732
William Morris wrote:
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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #2
Aww, geez. I got so focused where I was that I spaced off the JOIN
function. That'll do, thanks!

- Wm
--
William Morris
Semster, Seamlyne reProductions
Visit our website, http://www.seamlyne.com, for the most comfortable
historically inspired clothing you can buy!

"Bob Barrows" <re******@NOyahoo.SPAMcom> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
William Morris wrote:
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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: MIA | last post by:
Hi, I have an nxn array in which in each row is consisting of +ve and -ve numbers, such that -ve number comes before +ve numbers. e.g. -45 | -9 | -3 | 2 =================== -5 | -9 | 21 |...
5
by: Richard Berg | last post by:
Hello, I need to search a byte array for a sequence of bytes. The sequence may include wildcards. For example if the array contains 0xAA, 0xBB, 0xAA, OxDD then I want to be able to search for...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
46
by: RoSsIaCrIiLoIA | last post by:
Write a function that gets an array of unsigned int fill it with random values all differents, and sorts it. It should be faster than qsort too. Do you like my solution? _______________________...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
8
by: Jan | last post by:
I've got the following array: FileIn(Counter) How can i search the array for example "window" TIA
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
9
by: Rick | last post by:
I have a large list of objects where each object has a unique (non-overlapping) date range. The list is sorted chronologically. What is the most efficient way to search this list for a single...
11
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.