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.

258 Chap. 7 Internal Sorting7.2 Write an Insertion Sort algorithm for integer key values. However, here’sthe c<strong>at</strong>ch: The input is a stack (not an array), <strong>and</strong> the only variables th<strong>at</strong>your algorithm may use are a fixed number of integers <strong>and</strong> a fixed number ofstacks. The algorithm should return a stack containing the records in sortedorder (with the least value being <strong>at</strong> the top of the stack). Your algorithmshould be Θ(n 2 ) in the worst case.7.3 The Bubble Sort implement<strong>at</strong>ion has the following inner for loop:for (int j=n-1; j>i; j--)Consider the effect of replacing this with the following st<strong>at</strong>ement:for (int j=n-1; j>0; j--)Would the new implement<strong>at</strong>ion work correctly? Would the change affect theasymptotic complexity of the algorithm? How would the change affect therunning time of the algorithm?7.4 When implementing Insertion Sort, a binary search could be used to loc<strong>at</strong>ethe position within the first i − 1 elements of the array into which elementi should be inserted. How would this affect the number of comparisons required?How would using such a binary search affect the asymptotic runningtime for Insertion Sort?7.5 Figure 7.5 shows the best-case number of swaps for Selection Sort as Θ(n).This is because the algorithm does not check to see if the ith record is alreadyin the ith position; th<strong>at</strong> is, it might perform unnecessary swaps.(a) Modify the algorithm so th<strong>at</strong> it does not make unnecessary swaps.(b) Wh<strong>at</strong> is your prediction regarding whether this modific<strong>at</strong>ion actuallyimproves the running time?(c) Write two programs to compare the actual running times of the originalSelection Sort <strong>and</strong> the modified algorithm. Which one is actuallyfaster?7.6 Recall th<strong>at</strong> a sorting algorithm is said to be stable if the original ordering forduplic<strong>at</strong>e keys is preserved. Of the sorting algorithms Insertion Sort, BubbleSort, Selection Sort, Shellsort, Mergesort, Quicksort, Heapsort, Binsort,<strong>and</strong> Radix Sort, which of these are stable, <strong>and</strong> which are not? For each one,describe either why it is or is not stable. If a minor change to the implement<strong>at</strong>ionwould make it stable, describe the change.7.7 Recall th<strong>at</strong> a sorting algorithm is said to be stable if the original ordering forduplic<strong>at</strong>e keys is preserved. We can make any algorithm stable if we alterthe input keys so th<strong>at</strong> (potentially) duplic<strong>at</strong>e key values are made unique ina way th<strong>at</strong> the first occurrence of the original duplic<strong>at</strong>e value is less than thesecond occurrence, which in turn is less than the third, <strong>and</strong> so on. In the worstcase, it is possible th<strong>at</strong> all n input records have the same key value. Give an

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

Saved successfully!

Ooh no, something went wrong!