Professional Documents
Culture Documents
Sample Array Project: A Linear Search
Sample Array Project: A Linear Search
Searching
When working with arrays, it is often necessary to perform a search or "lookup" to determine whether an array contains a value that matches a certain key value. For example, the data from a file or database table will typically contain a "key" field
(such as a code), along with corresponding data fields (such as a description). It is often convenient to work with such data by first loading it into a UDT array in an initial routine, and then later pulling data from the array given a key value to search
on.
Two versions of a sample project to look up a state abbreviation and return the corresponding state name will now be presented. The first version demonstrates how to perform a serial search (also called a linear search) on an array; the second
demonstrates how to perform a binary search.
These projects will process a sequential file of state abbreviations and corresponding state names called STATES.DAT, with the following contents:
"AK","ALASKA"
"AL","ALABAMA"
.
.
.
"WV","WEST VIRGINIA"
"WY","WYOMING"
Option Explicit
Place the following code in the Form_Load event to initialize the state record array with the data from the file:
intStateUB = -1
Do Until EOF(intStateFileNbr)
intStateUB = intStateUB + 1
ReDim Preserve maudtStateRecord(0 To intStateUB)
With maudtStateRecord(intStateUB)
Input #intStateFileNbr, .StateAbbrev, .StateName
End With
Loop
End Sub
strTestName = GetStateName(strTestAbbrev)
Print
If strTestName = "" Then
Print "No state name found for '" & strTestAbbrev & "'."
Else
Print "'" & strTestAbbrev & "' is the abbreviation for '" _
& strTestName & "'."
End If
End Sub
· Code the GetStateName function, which uses a serial search to find the state name, given the state abbreviation:
End Function
The technique used here is called a serial search, because the StateAbbrev elements of the maudtStateRecord UDT array are compared one by one to the state abbreviation being looked for (pstrTestAbbrev) until either a match is found or all
elements of the array are examined without finding a match. If a match on the state abbreviation is found, the corresponding state name is returned.
· Run the project. Upon each click of the Try It button, input a valid or invalid state abbreviation. After several inputs, your form will look something like this:
The serial search is fine for small or unsorted arrays, but for larger arrays, serial searching is inefficient. If an array is sorted, the very efficient technique of binary searching can be used. In a binary search, the element at the middle of the array is
checked first. If a match is found, the search ends. Otherwise, the program determines whether the array argument needed is in the upper half of the array or the lower half. The half of the array containing the array argument is again halved and the
search continues as before. The term "binary" here refers to this process of dividing by two.
To build the binary search project, follow the exact same steps as the serial search example, except for the coding of the GetStateName function, which should be coded as follows:
intMinIndex = 0
intMaxIndex = UBound(maudtStateRecord)
End Function