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.

Open “SHOEPART.TXT”<br />

For Input As #1<br />

Do While (Not EOF(1)) And (numParts < UBound(part))<br />

numParts = numParts + 1<br />

Input #1, part(numParts)<br />

Loop<br />

Close #1<br />

End Sub<br />

Private Sub ShowData()<br />

Dim i As Integer ‘Display sorted list of parts<br />

picParts.Cls<br />

For i = 1 To numParts<br />

picParts.Print part(i),<br />

If i Mod 5 = 0 Then ‘only put 5 items per line<br />

picParts.Print<br />

End If<br />

Next i<br />

End Sub<br />

Private Sub SortData()<br />

Dim gap As Integer, doneFlag As Boolean<br />

Dim index As Integer, temp As String<br />

‘Shell sort shoe parts<br />

gap = Int(numParts / 2)<br />

Do While gap >= 1<br />

Do<br />

doneFlag = True<br />

For index = 1 To numParts - gap<br />

If part(index) > part(index + gap)<br />

Then temp = part(index)<br />

part(index) = part(index + gap)<br />

part(index + gap) = temp<br />

doneFlag = False<br />

End If<br />

Next index<br />

Loop Until doneFlag = True ‘Can also be written Loop Until doneFlag<br />

gap = Int(gap / 2) ‘Halve the length of the gap<br />

Loop<br />

End Sub<br />

[Run <strong>and</strong> click the comm<strong>and</strong> button.]<br />

■ SEARCHING<br />

Suppose we had an array of 1000 names in alphabetical order <strong>and</strong> wanted to locate a specific<br />

person in the list. One approach would be to start with the first name <strong>and</strong> consider each name<br />

until a match was found. This process is called a sequential search. We would find a person<br />

Sorting <strong>and</strong> Searching 191

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

Saved successfully!

Ooh no, something went wrong!