11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

232 Chap. 7 Internal Sorting59 20 17 13 28 14 23 83 36 9811 70 65 41 42 153620111328142315599817706541428328 14 11 13 36 20 17 1559 41 23 70 6598 42 831113171423152820364142705983659811 13 14 15 17 20 23 28 36 41 42 59 65 70 83 98Figure 7.6 An example of Shellsort. Sixteen items are sorted in four passes.The first pass sorts 8 sublists of size 2 <strong>and</strong> increment 8. The second pass sorts4 sublists of size 4 <strong>and</strong> increment 4. The third pass sorts 2 sublists of size 8 <strong>and</strong>increment 2. The fourth pass sorts 1 list of size 16 <strong>and</strong> increment 1 (a regularInsertion Sort).sublists of 2 elements each, where the array index of the 2 elements in each sublistdiffers by n/2. If there are 16 elements in the array indexed from 0 to 15, therewould initially be 8 sublists of 2 elements each. The first sublist would be the elementsin positions 0 <strong>and</strong> 8, the second in positions 1 <strong>and</strong> 9, <strong>and</strong> so on. Each list oftwo elements is sorted using Insertion Sort.The second pass of Shellsort looks <strong>at</strong> fewer, bigger lists. For our example thesecond pass would have n/4 lists of size 4, with the elements in the list being n/4positions apart. Thus, the second pass would have as its first sublist the 4 elementsin positions 0, 4, 8, <strong>and</strong> 12; the second sublist would have elements in positions 1,5, 9, <strong>and</strong> 13; <strong>and</strong> so on. Each sublist of four elements would also be sorted usingan Insertion Sort.The third pass would be made on two lists, one consisting of the odd positions<strong>and</strong> the other consisting of the even positions.The culmin<strong>at</strong>ing pass in this example would be a “normal” Insertion Sort of allelements. Figure 7.6 illustr<strong>at</strong>es the process for an array of 16 values where the sizesof the increments (the distances between elements on the successive passes) are 8,4, 2, <strong>and</strong> 1. Figure 7.7 presents a Java implement<strong>at</strong>ion for Shellsort.Shellsort will work correctly regardless of the size of the increments, providedth<strong>at</strong> the final pass has increment 1 (i.e., provided the final pass is a regular InsertionSort). If Shellsort will always conclude with a regular Insertion Sort, then howcan it be any improvement on Insertion Sort? The expect<strong>at</strong>ion is th<strong>at</strong> each of the(rel<strong>at</strong>ively cheap) sublist sorts will make the list “more sorted” than it was before.

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

Saved successfully!

Ooh no, something went wrong!