Archive for September, 2007

Msn web hosting - 264 Arrays Chapter 7 76 else if (

Sunday, September 16th, 2007

264 Arrays Chapter 7 76 else if ( key < array[ middle ] ) 77 high = middle - 1; // search low end of array 78 else 79 low = middle + 1; 80 81 } // end BinarySearch 82 83 return -1; // search key not found 84 85 } // end method BinarySearch 86 87 public void BuildOutput( 88 int[] array, int low, int mid, int high ) 89 { 90 for ( int i = 0; i < array.Length; i++ ) 91 { 92 if( i < low || i > high ) 93 outputLabel.Text += ” “; 94 95 // mark middle element in output 96 else if ( i == mid ) 97 outputLabel.Text += 98 array[ i ].ToString( “00″ ) + “* “; 99 else 100 outputLabel.Text += 101 array[ i ].ToString( “00″ ) + ” “; 102 } 103 104 outputLabel.Text += “n”; 105 106 } // end BuildOutput 107 108 } // end class BinarySearchTest Fig. 7.12 Binary search of a sorted array. (Part 3 of 4.)
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web hosting uk - Chapter 7 Arrays 263 23 private System.ComponentModel.Container components

Sunday, September 16th, 2007

Chapter 7 Arrays 263 23 private System.ComponentModel.Container components = null; 24 25 int[] a = { 0, 2,4, 6, 8,10,12,14,16, 26 18, 20, 22, 24, 26, 28 }; 27 28 // Visual Studio .NET generated code 29 30 // main entry point for application 31 [STAThread] 32 static void Main() 33 { 34 Application.Run( new BinarySearchTest() ); 35 } 36 37 // searches for an element by calling 38 // BinarySearch and displaying results 39 private void findButton_Click( object sender, 40 System.EventArgs e ) 41 { 42 int searchKey = Int32.Parse( inputTextBox.Text ); 43 44 // initialize display string for the new search 45 outputLabel.Text = “Portions of array searchedn”; 46 47 // perform the binary search 48 int element = BinarySearch( a, searchKey ); 49 50 if ( element != -1 ) 51 displayLabel.Text = “Found value in element ” + 52 element; 53 else 54 displayLabel.Text = “Value not found”; 55 56 } // end findButton_Click 57 58 // searchs array for specified key 59 public int BinarySearch( int[] array, int key ) 60 { 61 int low = 0; // low subscript 62 int high = array.Length - 1; // high subscript 63 int middle; // middle subscript 64 65 while ( low <= high ) 66 { 67 middle = ( low + high ) / 2; 68 69 // the following line displays the portion 70 // of the array currently being manipulated during 71 // each iteration of the binary search loop 72 BuildOutput( a, low, middle, high ); 73 74 if ( key == array[ middle ] ) // match 75 return middle; Fig. 7.12 Binary search of a sorted array. (Part 2 of 4.)
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

262 Arrays Chapter 7 peated in one quarter (Space web hosting)

Saturday, September 15th, 2007

262 Arrays Chapter 7 peated in one quarter of the original array. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that is not equal to the search key (i.e., the search key is not found). In a worst-case scenario, searching an array of 1024 elements will take only 10 comparisons by using a binary search. Repeatedly dividing 1024 by 2 (after each comparison we eliminate from consideration half the array) yields the values 512, 256, 128, 64, 32, 16, 8, 4, 2 and 1. The number 1024 (210) is divided by 2 only ten times to get the value 1. Dividing by 2 is equivalent to one comparison in the binary search algorithm. An array of 1,048,576 (220) elements takes a maximum of 20 comparisons to find the key. An array of one billion elements takes a maximum of 30 comparisons to find the key. This is a tremendous increase in performance over the linear search, which required comparing the search key with an average of half the elements in the array. For a one-billion-element array, the difference is between an average of 500 million comparisons and a maximum of 30 comparisons! The maximum number of comparisons needed for the binary search of any sorted array is the exponent of the first power of 2 greater than the number of elements in the array. Figure 7.12 presents the iterative version of method BinarySearch (lines 59 85). The method receives two arguments an integer array called array (the array to search) and an integer key (the search key). The array is passed to BinarySearch even though the array is an instance variable of the class. Once again, this is done because an array normally is passed to a method of another class for searching. Line 67 calculates the middle element of the array being searched by determining the number of elements in the array and dividing this value by 2. Recall that using the / operator with integers performs an integer division, which truncates the result. So, when there is an even number of elements in the array there is no middle element the middle of our array is actually between two elements. When this occurs, the calculation on line 67 returns the smaller index of the two middle elements. 1 // Fig. 7.12: BinarySearchTest.cs 2 // Demonstrating a binary search of an array. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class BinarySearchTest : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Label promptLabel; 14 15 private System.Windows.Forms.TextBox inputTextBox; 16 17 private System.Windows.Forms.Label resultLabel; 18 private System.Windows.Forms.Label displayLabel; 19 private System.Windows.Forms.Label outputLabel; 20 21 private System.Windows.Forms.Button findButton; 22 Fig. 7.12 Binary search of a sorted array. (Part 1 of 4.)
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Web hosting domain - Chapter 7 Arrays 261 24 Application.Run( new LinearSearcher()

Friday, September 14th, 2007

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.

260 Arrays Chapter 7 7.8 Searching (Web design tools) Arrays: Linear

Thursday, September 13th, 2007

260 Arrays Chapter 7 7.8 Searching Arrays: Linear Search and Binary Search Often, programmers work with large amounts of data stored in arrays. It might be necessary in this case to determine whether an array contains a value that matches a certain key value. The process of locating a particular element value in an array is called searching. In this section, we discuss two searching techniques the simple linear search technique and the more efficient binary search technique. Exercises 7.8 and 7.9 at the end of this chapter ask you to implement recursive versions of the linear and binary search. 7.8.1 Searching an Array with Linear Search In the program in Fig. 7.11, method LinearSearch (defined on lines 44 54) uses a for structure containing an ifstructure to compare each element of an array with a search key (line 44). If the search key is found, the method returns the subscript value for the element to indicate the exact position of the search key in the array. If the search key is not found, the method returns 1. (The value 1 is a good choice because it is not a valid subscript number.) If the elements of the array being searched are not in any particular order, it is just as likely that the value will be found in the first element as in the last. On average, the program will have to compare the search key with half the elements of the array. The program contains a 100-element array filled with the even integers from 0 198. The user types the search key in a TextBox (called inputTextBox) and clicks the findButton to start the search. [Note: The array is passed to LinearSearch even though the array is an instance variable of the class. This is done because an array normally is passed to a method of another class for searching.] 1 // Fig. 7.11: LinearSearcher.cs 2 // Demonstrating linear searching of an array. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class LinearSearcher : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button searchButton; 13 private System.Windows.Forms.TextBox inputTextBox; 14 private System.Windows.Forms.Label outputLabel; 15 16 int[] a = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 17 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 }; 18 19 // Visual Studio .NET generated code 20 21 [STAThread] 22 static void Main() 23 { Fig. 7.11 Linear search of an array. (Part 1 of 2.)
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Chapter 7 (Web site design) Arrays 259 59 hold = c[

Wednesday, September 12th, 2007

Chapter 7 Arrays 259 59 hold = c[ first ]; 60 c[ first ] = c[ first + 1 ]; 61 c[ first + 1 ] = hold; 62 } 63 } Fig. 7.10 Sorting an array with bubble sort. (Part 3 of 3.) Method BubbleSort receives the array as parameter b. The nested for loop on lines 46 51 performs the sort. The outer loop controls the number of passes of the array. The inner loop controls the comparisons and necessary swapping of the elements during each pass. Method BubbleSort first compares b[0]to b[1], then b[1]to b[2], then b[2] to b[3] and so on, until it completes the pass by comparing b[8] to b[9]. Although there are 10 elements, the comparison loop performs only nine comparisons. As a result of the way the successive comparisons are made, a large value may move down the array (sink) many positions (and sometimes all the way to the bottom of the array) on a single pass. However, a small value may move up (bubble) only one position. On the first pass, the largest value is guaranteed to sink to the bottom element of the array, b[9]. On the second pass, the second largest value is guaranteed to sink to b[8]. On the ninth pass, the ninth largest value sinks to b[1]. This leaves the smallest value in b[0], so only nine passes are needed to sort a 10-element array. If a comparison reveals that the two elements appear in descending order, BubbleSort calls Swap to exchange the two elements so they will be in ascending order in the array. Method Swap receives a reference to the array (which it calls c) and one integer representing the subscript of the first element of the array to be exchanged. Three assignments on lines 59 61 perform the exchange, where the extra variable hold temporarily stores one of the two values being swapped. The swap cannot be performed with only the two assignments c[ first ] = c[ first + 1 ]; c[ first + 1 ] = c[ first ]; If c[first]is 7 and c[first+1]is 5, after the first assignment, both elements of the array contain 5 and the value 7 is lost hence, the need for the extra variable hold. The advantage of the bubble sort is that it is easy to program. However, the bubble sort runs slowly, which becomes apparent when sorting large arrays. More advanced courses (often titled Data Structures or Algorithms or Computational Complexity ) investigate sorting and searching in greater depth. Note that the .NET framework includes a built- in array-sorting capability that implements a high-speed sort. To sort the array a in Fig. 7.10, you can use the statement Array.Sort( a );
We recommend high quality webhost to host and run your jsp application: christian web host services.

258 Arrays Chapter 7 6 using System.ComponentModel; 7 (Web server version)

Tuesday, September 11th, 2007

258 Arrays Chapter 7 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 public class BubbleSorter : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button sortButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 [STAThread] 18 static void Main() 19 { Application.Run( new BubbleSorter() ); 21 } 22 23 private void sortButton_Click( object sender, 24 System.EventArgs e ) 25 { 26 int[] a = { 2,6,4,8,10,12,89,68,45,37 }; 27 28 outputLabel.Text = “Data items in original ordern”; 29 for ( int i = 0; i < a.Length; i++ ) 31 outputLabel.Text += " " + a[ i ]; 32 33 // sort elements in array a 34 BubbleSort( a ); 35 36 outputLabel.Text += "nnData items in ascending ordern"; 37 38 for ( int i = 0; i < a.Length; i++ ) 39 outputLabel.Text += " " + a[ i ]; 41 } // end method sortButton_Click 42 43 // sort the elements of an array with bubble sort 44 public void BubbleSort( int[] b ) 45 { 46 for ( int pass = 1; pass < b.Length; pass++ ) // passes 47 48 for ( int i = 0; i < b.Length - 1; i++ ) // one pass 49 if ( b[ i ] > b[ i + 1 ] ) // one comparison 51 Swap( b, i ); // one swap 52 } 53 54 // swap two elements of an array 55 public void Swap( int[] c, int first ) 56 { 57 int hold; // temporary holding area for swap 58 Fig. 7.10 Sorting an array with bubble sort. (Part 2 of 3.)
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Msn web hosting - Chapter 7 Arrays 257 Software Engineering Observation 7.1

Monday, September 10th, 2007

Chapter 7 Arrays 257 Software Engineering Observation 7.1 When a method receives a reference-type object parameter by value, the object is not passed by value the object still passes by reference. Rather, the object s reference is passed by value. This prevents a method from overwriting references passed to that method. In the vast majority of cases, protecting the caller s reference from modification is the desired behavior. If you encounter a situation where you truly want the called procedure to modify the caller s reference, pass the reference-type using keyword ref but, again, such situations are rare. Software Engineering Observation 7.2 In C#, reference-type objects (including arrays) always pass by reference. So, a called procedure receiving a reference to an object in a caller can change the caller s object. 7.7 Sorting Arrays Sorting data (i.e., arranging the data into some particular order, such as ascending or descending) is one of the most important computing applications. A bank sorts all checks by account number so that it can prepare individual bank statements at the end of each month. Telephone companies sort their lists of accounts by last name, and within that, by first name to make it easy to find phone numbers. Virtually every organization must sort some data, and in many cases, massive amounts of it. Sorting data is an intriguing problem that has attracted some of the most intense research efforts in the computer science field. In this section, we discuss one of the simplest sorting schemes. In the exercises, we investigate more sophisticated sorting algorithms. Performance Tip 7.2 Sometimes, the simplest algorithms perform poorly. Their virtue is that they are easy to write, test and debug. Complex algorithms sometimes are needed to realize maximum performance of a program. Figure 7.10 sorts the values of the 10-element array a into ascending order. The technique we use is called the bubble sort, because smaller values gradually bubble their way to the top of the array (i.e., toward the first element) like air bubbles rising in water. The technique sometimes is called the sinking sort, because the larger values sink to the bottom of the array. Bubble sort uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the values remain in the same order. If a pair is in decreasing order, the bubble sort swaps the values in the array. The program contains methods Main, BubbleSort and Swap. Method sortButton_Click (lines 23 41) creates array a, invokes BubbleSort and displays output. Line 34 of sortButton_Click invokes method BubbleSort (lines 44 52) to sort array a. Line 51 in method BubbleSort calls method Swap (lines 55 62) to exchange two elements of the array. 1 // Fig. 7.10: BubbleSorter.cs 2 // Sorting an array’s values into ascending order. 3 using System; 4 using System.Drawing; 5 using System.Collections; Fig. 7.10 Sorting an array with bubble sort. (Part 1 of 3.)
We recommend high quality webhost to host and run your jsp application: christian web host services.

Most popular web site - 256 Arrays Chapter 7 102 // create new

Sunday, September 9th, 2007

256 Arrays Chapter 7 102 // create new reference and assign it to array 103 array = new int[] { 11, 12, 13 }; 104 } 105 106 // modify elements of array and change reference array 107 // to refer to a new array 108 void SecondDouble( ref int[] array ) 109 { 110 // double each element’s value 111 for ( int i = 0; i < array.Length; i++ ) 112 array[ i ] *= 2; 113 114 // create new reference and assign it to array 115 array = new int[] { 11, 12, 13 }; 116 } 117 } Fig. 7.9 Passing an array reference by value and by reference (Part 3 of 3.). Lines 60 90 perform similar tests, using array variables secondArray and secondArrayCopy and method SecondDouble (lines 108 116). Method Second- Doubleperforms the same operations as FirstDouble, but receives its array argument using keyword ref. In this case, the reference stored in secondArray after the method call is a reference to the array allocated on line 115 of SecondDouble, demonstrating that a reference passed with keyword ref can be modified by the called method so that the reference actually points to a different object, in this case an array allocated in procedure SecondDouble. The if/else structure in lines 85 90 demonstrates that second- Array and secondArrayCopy no longer refer to the same array.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web hosting asp - Chapter 7 Arrays 255 51 // test whether

Sunday, September 9th, 2007

Chapter 7 Arrays 255 51 // test whether reference was changed by FirstDouble 52 if ( firstArray == firstArrayCopy ) 53 outputLabel.Text += 54 “nnThe references refer to the same arrayn”; 55 else 56 outputLabel.Text += 57 “nnThe references refer to different arraysn”; 58 59 // create and initialize secondArray 60 int[] secondArray = { 1, 2, 3 }; 61 62 // copy secondArray reference 63 int[] secondArrayCopy = secondArray; 64 65 outputLabel.Text += “nTest passing secondArray ” + 66 “reference by reference”; 67 68 outputLabel.Text += “nnContents of secondArray ” + 69 “before calling SecondDouble:nt”; 70 71 // print contents of secondArray before method call 72 for ( int i = 0; i < secondArray.Length; i++ ) 73 outputLabel.Text += secondArray[ i ] + " "; 74 75 SecondDouble( ref secondArray ); 76 77 outputLabel.Text += "nnContents of secondArray " + 78 "after calling SecondDouble:nt"; 79 80 // print contents of secondArray after method call 81 for ( int i = 0; i < secondArray.Length; i++ ) 82 outputLabel.Text += secondArray[ i ] + " "; 83 84 // test whether reference was changed by SecondDouble 85 if ( secondArray == secondArrayCopy ) 86 outputLabel.Text += 87 "nnThe references refer to the same arrayn"; 88 else 89 outputLabel.Text += 90 "nnThe references refer to different arraysn"; 91 92 } // end method showOutputButton_Click 93 94 // modify elements of array and attempt to modify 95 // reference 96 void FirstDouble( int[] array ) 97 { 98 // double each element's value 99 for ( int i = 0; i < array.Length; i++ ) 100 array[ i ] *= 2; 101 Fig. 7.9 Passing an array reference by value and by reference (Part 2 of 3.).
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.