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.

The Shell sort is illustrated in what follows. Crossing arrows indicate that a swap occurred.<br />

Initial Gap = Int([Number of Items] / 2) = Int(5 / 2) = 2<br />

Because there was a swap, use the same gap for the second pass.<br />

Again, there was a swap, so keep the current gap.<br />

There were no swaps for the current gap of 2, so<br />

Next Gap = Int([Previous Gap] / 2) = Int(2 / 2) = 1<br />

Because there was a swap (actually two swaps), keep the same gap.<br />

Because there were no swaps for the current gap, then<br />

Next Gap = Int([Previous Gap] / 2) = Int(1 / 2) = 0<br />

<strong>and</strong> the Shell sort is complete.<br />

Notice that the Shell sort required 14 comparisons to sort the list whereas the bubble sort<br />

required only 10 comparisons for the same list. This illustrates the fact that for very short<br />

lists, the bubble sort is preferable; however, for lists of 30 items or more, the Shell sort will<br />

consistently outperform the bubble sort. Table 6.3 shows the average number of comparisons<br />

required to sort arrays of varying sizes.<br />

Sorting <strong>and</strong> Searching 189

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

Saved successfully!

Ooh no, something went wrong!