Web server logs - Chapter 7 Arrays 265 Fig. 7.12 Binary search

Chapter 7 Arrays 265 Fig. 7.12 Binary search of a sorted array. (Part 4 of 4.) If key matches the middle element of a subarray (line 74), BinarySearch returns middle(the subscript of the current element), indicating that the value was found and the search is complete. If key does not match the middle element of a subarray, Binary- Search adjusts the low subscript or highsubscript (both declared in the method) so that a smaller subarray can be searched. If key is less than the middle element (line 76), the high subscript is set to middle-1, and the search continues on the elements from low to middle-1. If key is greater than the middle element (line 78), the low subscript is set to middle+1, and the search continues on the elements from middle+1to high. These comparisons occur in the nested if/else structure on lines 74 79. The program uses a 15-element array. The first power of 2 greater than the number of array elements is 16 (24) so at most four comparisons are required to find the key. To illustrate this concept, method BinarySearch calls method BuildOutput (lines 87 106) to output each subarray during the binary search process. BuildOutput marks the middle element in each subarray with an asterisk (*) to indicate the element with which the key is compared. Each search in this example results in a maximum of four lines of output one per comparison. Note that the .NET framework includes a built-in array- searching capability that implements the binary-search algorithm. To search for the key 7 in the sorted array a in Fig. 7.12, you can use the statement Array.BinarySearch( a, 7 ); 7.9 Multiple-Subscripted Arrays So far we have studied single-subscripted (or one-dimensional) arrays i.e., those that contain single lists of values. In this section, we introduce multiple-subscripted (often called multidimensional) arrays. Such arrays require two or more subscripts to identify particular elements. Arrays that require two subscripts to identify a particular element commonly are called double-subscripted arrays. We concentrate on double-subscripted arrays (often called two-dimensional arrays). There are two types of multiple-subscripted arrays rectangular and jagged. Rectangular arrays with two subscripts often represent tables of values consisting of information arranged in rows and columns, where each row is the same size, and each column is the same size. To identify a particular table element, we must specify the two subscripts by convention, the first identifies the element s row and the second identifies the element s column. Multiple-subscripted arrays can have more than two subscripts. Figure 7.13 illustrates a double-subscripted array, a, containing three rows and four columns (i.e., a 3-by-4 array). An array with m rows and n columns is called an m-by-n array.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply