30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

290 Arrays Chapter 7<br />

The declaration of an array creates a variable th at can s<strong>to</strong>re a reference <strong>to</strong> an array but does not<br />

create the array in memory.<br />

Arrays can be declared <strong>to</strong> contain elements of any data type.<br />

Arrays are represented as objects in <strong>Visual</strong> Ba sic, so they must also be allocated with keyword<br />

New. The value s<strong>to</strong>red in the array variable is a reference <strong>to</strong> the location in the computer’s memory<br />

where the array object is created.<br />

Array bounds determine what indices can be used <strong>to</strong> access an element in the array.<br />

The initializer list enclosed in braces ( { and }) specifies the initial values of the elements in the<br />

array. The initializer list can contain a comma-separated list specifying the initial values of the elements<br />

in the array. If the initializer list is empty, the elements in the array are initialized <strong>to</strong> the<br />

default value for the data type of the array.<br />

Keyword Nothing denotes an empty reference (i.e., a value indicating that a reference variable<br />

has not been assigned an address in the computer’s memory).<br />

Unlike languages such as C and C++, <strong>Visual</strong> <strong>Basic</strong> provides mechanisms <strong>to</strong> prevent the accessing<br />

of elements that are outside the bounds of an array.<br />

If a program attempts <strong>to</strong> use an invalid index (i.e., an index outside the bounds of an array), <strong>Visual</strong><br />

<strong>Basic</strong> generates an exception.<br />

To pass an array argument <strong>to</strong> a procedure, specify the name of the array and do not include parentheses.<br />

Although entire arrays are passed by reference, individual array elements of primitive data types<br />

can be passed by value.<br />

To pass an array element <strong>to</strong> a procedure, use the indexed name of the array element as an argument<br />

in the procedure call.<br />

The sorting of data (i.e., the arranging of data in<strong>to</strong> some particular order, such as ascending or descending<br />

order) is one of the most important computing applications.<br />

A bubble sort makes several passes through the array. Each pass compares successive pairs of elements.<br />

On an ascending bubble sort, if a pair is in increasing order (or the values are equal), the<br />

bubble sort leaves the values as they are; if a pair is in decreasing order, the bubble sort swaps their<br />

values in the array.<br />

The advantage of the bubble sort is that it is easy <strong>to</strong> program. <strong>How</strong>ever, the bubble sort runs slowly,<br />

as becomes apparent during the sorting of large arrays.<br />

The linear search algorithm compares each element of an array against a search key. If the elements<br />

of the array being searched are not in any particular order, it is just as likely that the value<br />

will be found in the first element as in the last. Thus, the procedure compares the search key with<br />

half the elements of the array, on average. Linear search works well for small arrays and is acceptable<br />

even for large unsorted arrays.<br />

For sorted arrays, the binary search algorithm eliminates from consideration half the elements in<br />

the array after each comparison. The algorithm locates the middle array element and compares it<br />

with the search key. If they are equal, the search key has been found, and the index of that element<br />

is returned. Otherwise, the problem is reduced <strong>to</strong> searching half of the array. If the search key is<br />

less than the middle array element, the first half of the array is searched; otherwise, the second half<br />

of the array is searched.<br />

In a worst-case scenario, searching an array of 1024 elements via binary search requires only 10<br />

comparisons. The maximum number of comparisons needed <strong>to</strong> complete a binary search of any<br />

sorted array is indicated by the exponent of the first power of two that is greater than or equal <strong>to</strong><br />

the number of elements in the array.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!