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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

262 Chap. 7 Internal SortingModify the Bubble Sort implement<strong>at</strong>ion to add this flag <strong>and</strong> test. Comparethe modified implement<strong>at</strong>ion on a range of inputs to determine if it does ordoes not improve performance in practice.7.2 Double Insertion Sort is a vari<strong>at</strong>ion on Insertion Sort th<strong>at</strong> works from themiddle of the array out. At each iter<strong>at</strong>ion, some middle portion of the arrayis sorted. On the next iter<strong>at</strong>ion, take the two adjacent elements to the sortedportion of the array. If they are out of order with respect to each other, thanswap them. Now, push the left element toward the right in the array so longas it is gre<strong>at</strong>er than the element to its right. And push the right elementtoward the left in the array so long as it is less than the element to its left.The algorithm begins by processing the middle two elements of the array ifthe array is even. If the array is odd, then skip processing the middle item<strong>and</strong> begin with processing the elements to its immedi<strong>at</strong>e left <strong>and</strong> right.First, explain wh<strong>at</strong> the cost of Double Insertion Sort will be in comparison tost<strong>and</strong>ard Insertion sort, <strong>and</strong> why. (Note th<strong>at</strong> the two elements being processedin the current iter<strong>at</strong>ion, once initially swapped to be sorted with with respectto each other, cannot cross as they are pushed into sorted position.) Then, implementDouble Insertion Sort, being careful to properly h<strong>and</strong>le both whenthe array is odd <strong>and</strong> when it is even. Compare its running time in practiceagainst st<strong>and</strong>ard Insertion Sort. Finally, explain how this speedup might affectthe threshold level <strong>and</strong> running time for a Quicksort implement<strong>at</strong>ion.7.3 Perform a study of Shellsort, using different increments. Compare the versionshown in Section 7.3, where each increment is half the previous one,with others. In particular, try implementing “division by 3” where the incrementson a list of length n will be n/3, n/9, etc. Do other increment schemeswork as well?7.4 The implement<strong>at</strong>ion for Mergesort given in Section 7.4 takes an array as input<strong>and</strong> sorts th<strong>at</strong> array. At the beginning of Section 7.4 there is a simplepseudocode implement<strong>at</strong>ion for sorting a linked list using Mergesort. Implementboth a linked list-based version of Mergesort <strong>and</strong> the array-basedversion of Mergesort, <strong>and</strong> compare their running times.7.5 Starting with the Java code for Quicksort given in this chapter, write a seriesof Quicksort implement<strong>at</strong>ions to test the following optimiz<strong>at</strong>ions on a widerange of input d<strong>at</strong>a sizes. Try these optimiz<strong>at</strong>ions in various combin<strong>at</strong>ions totry <strong>and</strong> develop the fastest possible Quicksort implement<strong>at</strong>ion th<strong>at</strong> you can.(a) Look <strong>at</strong> more values when selecting a pivot.(b) Do not make a recursive call to qsort when the list size falls below agiven threshold, <strong>and</strong> use Insertion Sort to complete the sorting process.Test various values for the threshold size.(c) Elimin<strong>at</strong>e recursion by using a stack <strong>and</strong> inline functions.

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

Saved successfully!

Ooh no, something went wrong!