12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

94 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>3.17 Given an array s<strong>to</strong>ring integers ordered by value, modify the binary searchroutine <strong>to</strong> return the position of the first integer with value K in the situationwhere K can appear multiple times in the array. Be sure that your algorithmis Θ(log n), that is, do not resort <strong>to</strong> sequential search once an occurrence ofK is found.3.18 Given an array s<strong>to</strong>ring integers ordered by value, modify the binary searchroutine <strong>to</strong> return the position of the integer with the greatest value less thanK when K itself does not appear in the array. Return ERROR if the leastvalue in the array is greater than K.3.19 Modify the binary search routine <strong>to</strong> support search in an array of infinitesize. In particular, you are given as input a sorted array <strong>and</strong> a key valueK <strong>to</strong> search for. Call n the position of the smallest value in the array thatis equal <strong>to</strong> or larger than X. Provide an algorithm that can determine n inO(log n) comparisons in the worst case. Explain why your algorithm meetsthe required time bound.3.20 It is possible <strong>to</strong> change the way that we pick the dividing point in a binarysearch, <strong>and</strong> still get a working search routine. However, where we pick thedividing point could affect the performance of the algorithm.(a) If we change the dividing point computation in function binary fromi = (l + r)/2 <strong>to</strong> i = (l + ((r − l)/3)), what will the worst-case runningtime be in asymp<strong>to</strong>tic terms? If the difference is only a constanttime fac<strong>to</strong>r, how much slower or faster will the modified program becompared <strong>to</strong> the original version of binary?(b) If we change the dividing point computation in function binary fromi = (l + r)/2 <strong>to</strong> i = r − 2, what will the worst-case running time be inasymp<strong>to</strong>tic terms? If the difference is only a constant time fac<strong>to</strong>r, howmuch slower or faster will the modified program be compared <strong>to</strong> theoriginal version of binary?3.21 Design an algorithm <strong>to</strong> assemble a jigsaw puzzle. Assume that each piecehas four sides, <strong>and</strong> that each piece’s final orientation is known (<strong>to</strong>p, bot<strong>to</strong>m,etc.). Assume that you have available a functionbool compare(Piece a, Piece b, Side ad)that can tell, in constant time, whether piece a connects <strong>to</strong> piece b on a’sside ad <strong>and</strong> b’s opposite side bd. The input <strong>to</strong> your algorithm should consis<strong>to</strong>f an n × m array of r<strong>and</strong>om pieces, along with dimensions n <strong>and</strong> m. Thealgorithm should put the pieces in their correct positions in the array. Youralgorithm should be as efficient as possible in the asymp<strong>to</strong>tic sense. Write

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

Saved successfully!

Ooh no, something went wrong!