Web hosting domain - Chapter 7 Arrays 261 24 Application.Run( new LinearSearcher()
Chapter 7 Arrays 261 24 Application.Run( new LinearSearcher() ); 25 } 26 27 private void searchButton_Click( object sender, 28 System.EventArgs e ) 29 { 30 int searchKey = Int32.Parse( inputTextBox.Text ); 31 32 int elementIndex = LinearSearch( a, searchKey ); 33 34 if ( elementIndex != -1 ) 35 outputLabel.Text = 36 “Found value in element ” + elementIndex; 37 38 else 39 outputLabel.Text = “Value not found”; 40 41 } // end method searchButton_Click 42 43 // search array for the specified key value 44 public int LinearSearch( int[] array, int key ) 45 { 46 for ( int n = 0; n < array.Length; n++ ) 47 { 48 if ( array[ n ] == key ) 49 return n; 50 } 51 52 return -1; 53 54 } // end method LinearSearch 55 } Fig. 7.11 Linear search of an array. (Part 2 of 2.) 7.8.2 Searching a Sorted Array with Binary Search The linear search method works well for small or unsorted arrays. However, for large arrays, linear searching is inefficient. If the array is sorted, the high-speed binary search technique can be used. The binary search algorithm eliminates half of the elements in the array being searched after each comparison. The algorithm locates the middle array element and compares it with the search key. If they are equal, the search key has been found, and the subscript of that element is returned. Otherwise, the problem is reduced to searching half of the array. If the search key is less than the middle array element, the first half of the array is searched; otherwise, the second half of the array is searched. If the search key is not the middle element in the specified subarray (a piece of the original array), the algorithm is re
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.