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.

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

Private Sub SwapData(index As Integer)<br />

‘Swap entries<br />

Call SwapStr(city(index), city(index + 1))<br />

Call SwapNum(pop(index), pop(index + 1))<br />

Call SwapNum(income(index), income(index + 1))<br />

Call SwapNum(natives(index), natives(index + 1))<br />

Call SwapNum(advDeg(index), advDeg(index + 1))<br />

End Sub<br />

Private Sub SwapNum(a As Single, b As Single)<br />

Dim temp As Single<br />

‘Interchange values of a <strong>and</strong> b<br />

temp = a<br />

a = b<br />

b = temp<br />

End Sub<br />

Private Sub SwapStr(a As String, b As String)<br />

Dim temp As String<br />

‘Interchange values of a <strong>and</strong> b<br />

temp = a<br />

a = b<br />

b = temp<br />

End Sub<br />

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

■ SHELL SORT<br />

The bubble sort is easy to underst<strong>and</strong> <strong>and</strong> program. However, it is too slow for really long<br />

lists. The Shell sort, named for its inventor, Donald L. Shell, is much more efficient in such<br />

cases. It compares distant items first <strong>and</strong> works its way down to nearby items. The interval separating<br />

the compared items is called the gap. The gap begins at one-half the length of the list<br />

<strong>and</strong> is successively halved until eventually each item is compared with its neighbor as in the<br />

bubble sort. The algorithm for a list of n items is as follows:<br />

1. Begin with a gap of g = Int(n / 2).<br />

2. Compare items 1 <strong>and</strong> 1 + g, 2 <strong>and</strong> 2 + g, . . . , n – g <strong>and</strong> n. Swap any pairs that<br />

are out of order.<br />

3. Repeat Step 2 until no swaps are made for gap g.<br />

4. Halve the value of g.<br />

5. Repeat Steps 2, 3, <strong>and</strong> 4 until the value of g is 0.

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

Saved successfully!

Ooh no, something went wrong!