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

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.

Leave a Reply