19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

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.

190 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

TABLE 6.3<br />

Efficiency of Bubble <strong>and</strong> Shell Sorts<br />

Array Elements Bubble Sort Comparisons Shell Sort Comparisons<br />

EXAMPLE 4<br />

5 10 15<br />

10 45 57<br />

15 105 115<br />

20 190 192<br />

25 300 302<br />

30 435 364<br />

50 1225 926<br />

100 4950 2638<br />

500 124,750 22,517<br />

1000 499,500 58,460<br />

Use the Shell sort to alphabetize the parts of a running shoe (see Figure 6-7).<br />

FIGURE 6-7 Running Shoe<br />

SOLUTION:<br />

In the following program, the data are read into an array that has been dimensioned so as to guarantee<br />

more than enough space. In the event procedure Form_Load, the variable numParts provides the subscripts<br />

for the array <strong>and</strong> serves as a counter. The final value of numParts is available to all procedures<br />

because the variable was created in the (Declarations) section of (General). The Sub procedure SortData<br />

uses a flag to indicate if a swap has been made during a pass.<br />

Dim part(1 To 50) As String<br />

Dim numParts As Integer<br />

Private Sub cmdDisplayParts_Click()<br />

‘Sort <strong>and</strong> display parts of running shoe<br />

Call SortData<br />

Call ShowData<br />

End Sub<br />

Private Sub Form_Load()<br />

‘Read part names numParts = 0 ‘Number of parts

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

Saved successfully!

Ooh no, something went wrong!