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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 Arrays 271<br />

Fig. Fig. 7.11 7.11 Sorting an array with bubble sort (part 3 of 3).<br />

The program contains methods cmdCreate_Click and cmdSort_Click.<br />

Method cmdCreate_Click (lines 26–43) assigns 10 random values <strong>to</strong> the elements of<br />

array and displays the contents of the array in txtOriginal. Method<br />

cmdSort_Click (lines 46–62) sorts array by calling procedure BubbleSort from<br />

modBubbleSort.<br />

Procedure BubbleSort receives the array as parameter sortArray. The nested<br />

For/Next structures in lines 10–20 of Fig. 7.10 performs the sort. The outer loop controls<br />

the number of passes of the array. The inner loop (lines 12–18) controls the comparisons<br />

and swapping (if necessary) of the elements during each pass.<br />

Procedure BubbleSort first compares sortArray(0) <strong>to</strong> sortArray(1), then<br />

sortArray(1) <strong>to</strong> sortArray(2), and so on until it completes the pass by comparing<br />

sortArray(8) <strong>to</strong> sortArray(9). Although there are 10 elements, the comparison<br />

loop performs only nine comparisons (because the comparisons each involve a pair of numbers).<br />

The comparisons performed in a bubble sort could cause a large value <strong>to</strong> move down<br />

the array (sink) many positions on a single pass. <strong>How</strong>ever, a small value cannot move up<br />

(bubble) more than one position per pass. On the first pass, the largest value is guaranteed<br />

<strong>to</strong> sink <strong>to</strong> the bot<strong>to</strong>m element of the array, sortArray(9). On the second pass, the<br />

second-largest value is guaranteed <strong>to</strong> sink <strong>to</strong> sortArray(8). On the ninth pass, the ninth<br />

largest value sinks <strong>to</strong> sortArray(1), leaving the smallest value in sortArray(0).<br />

Thus, only nine passes are required <strong>to</strong> sort a 10-element array (and, in general, only n-1<br />

passes are needed <strong>to</strong> sort an n-element array).<br />

If a comparison reveals that the two elements are in descending order, BubbleSort<br />

calls procedure Swap <strong>to</strong> exchange the two elements, placing them in ascending order in the<br />

array. Procedure Swap receives the array (which it calls swapArray) and the index of the<br />

first element of the array <strong>to</strong> transpose (with the subsequent element). The exchange is performed<br />

by three assignments<br />

hold = swapArray(first)<br />

swapArray(first) = swapArray(first + 1)<br />

swapArray(first + 1) = hold

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

Saved successfully!

Ooh no, something went wrong!