Searching Arrays |
|
|
Searching involves looking systemically through the elements of an array. There are many different searching techniques. In most cases you need to know not only if a value is in an array, but also where it is located in the array. ================== Linear Search In a linear search every element in the array is compared to a search key. Since the array is in no particular order, it is just as likely that the value will be found in the first element as in the last. On average, therefore, the program compares the search key with half the elements of the array to locate a value in the array. The linear search works well for small arrays or for unsorted arrays, but is inefficient for large, sorted arrays. In the following example, setOfValues is the array being searched, and searchValue is the value being searched for. The function index returns the subscript of the element containing the search value. Private Function searchArray(ByVal setOfValues()
As String, _ ================== The binary search requires that the array being searched is in sorted order. The algorithm removes from consideration one half of the elements in the array being searched after each comparison.
The maximum number of comparisons needed for the binary search of any sorted array can be determined by finding the first value of the power of 2 greater than or equal to the number of elements in the array.
================== Binary Overhead The tremendous performance gains of the binary search over the linear search do not come without a price. Sorting an array is an expensive operation compared to searching an entire array once for an item. The overhead of sorting an array becomes worthwhile when the array will need to be searched many times at high speed. ================== Iterative Example The figure below presents the iterative version of function binarySearch. If the searchKey does not match the middle element of a subarray, the low index or high index is adjusted so that a smaller subarray can be searched. If the searchKey is less than the middle element, the high index is set to middle - 1, and the search is continued on the elements from low to middle - 1. If the searchKey is greater than the middle element, the low index is set to middle + 1, and the search is continued on the elements from middle + 1 to high. --------- The example uses an array of 15 elements and a searchKey of 6. The middle element in each subarray is marked by a red border and an arrow to indicate the element to which the searchKey will be compared. The first power of 2 greater than the number of elements in this array is 16 (24), so a maximum of four comparisons are required to find the searchKey.
Private Function
binarySearch(ByVal mArray() As Integer, _ ================= A Recursive Binary Search The binary search algorithm presented above is better written as a recursive function. Recursion is a condition in which a function or sub procedure calls itself. The proper definition is: a recursive call is a procedure call in which the procedure being called is the same as the one making the call. A recursive algorithm reproduces itself with smaller and smaller instances of itself until a solution is found. The recursive algorithm is implemented by using a procedure that makes recursive calls to itself. A binary search requires that an array be in sorted order--either ascending or descending. In the recursive binary search, we first compare the key with the item in the middle position of the array. If there's a match, we can return immediately. If the key is less than the middle key, then the item sought must lie in the lower half of the array; if it's greater then the item sought must lie in the upper half of the array. So we repeat the procedure on the lower (or upper) half of the array. The binary search is recursive: it determines whether the search key lies in the lower or upper half of the array, then calls itself on the appropriate half. '***************************************************************************************** VB.Net arrays now provide the Contains method to perform a linear search.
|