solution+to+cormen

solution+to+cormen - Solutions for Introduction to...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Solutions for Introduction to algorithms second edition Philip Bille The author of this document takes absolutely no responsibility for the contents. This is merely a vague suggestion to a solution to some of the exercises posed in the book Introduction to algo- rithms by Cormen, Leiserson and Rivest. It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send a message to beetle@it.dk . It is important that you try hard to solve the exercises on your own. Use this document only as a last resort or to check if your instructor got it all wrong. Please note that the document is under construction and is updated only sporadically. Have fun with your algorithms. Best regards, Philip Bille Last update: December 9, 2002
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1.2 - 2 Insertion sort beats merge sort when 8n 2 < 64n lg n , n<8 lg n , 2 n/8 <n .Th isistruefor 2 6 n 6 43 (found by using a calculator). Rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time. 1 - 1 We assume that all months are 30 days and all years are 365 . 1 1 1 1 1 1 1 second minute hour day month year century lgn 2 10 6 2 6 · 7 2 36 · 8 2 864 · 8 2 2592 · 9 2 94608 · 10 2 · 12 n 10 12 36 · 10 14 1296 · 10 16 746496 · 10 6718464 · 10 18 8950673664 · 10 20 8950673664 · 10 24 n 10 6 6 · 10 7 36 · 10 8 864 · 10 8 2592 · 10 9 94608 · 10 94608 · 10 n lg n 62746 2801417 ?? ?? ?? ?? ?? n 2 10 3 24494897 6 · 10 4 293938 1609968 30758413 307584134 n 3 10 2 391 1532 4420 13736 98169 455661 2 n 19 25 31 36 41 49 56 n ! 9 11 12 13 15 17 18 2
Background image of page 2
2.1 - 2 In line 5 of INSERTION-SORT alter A [ i ] >k ey to A [ i ] <k in order to sort the elements in nonincreasing order. 2.1 - 3 Algorithm 1 LINEAR-SEARCH ( A, v ) Input: A = h a 1 ,a 2 ,...a n i and a value v . Output: An index i such that v = A [ i ] or nil if v 6∈ A for i 1 to n do if A [ i ]= v then return i end if end for return nil As a loop invariant we say that none of the elements at index A [ 1,. ..,i - 1 ] are equal to v . Clearly, all properties are fullFlled by this loop invariant. 2.2 - 1 n 3 /1000 - 100n 2 - 100n + 3 = Θ ( n 3 ) . 2.2 - 2 Assume that ±IND-MIN ( A, r, s ) returns the index of the smallest element in A between indices r and s . Clearly, this can be implemented in O ( s - r ) time if r > s . Algorithm 2 SELECTION-SORT ( A ) Input: A = h a 1 2 n i Output: sorted A . for i 1 to n - 1 do j ±IND-MIN ( A, i, n ) A [ j ] A [ i ] end for As a loop invariant we choose that A [ - 1 ] are sorted and all other elements are greater than these. We only need to iterate to n - 1 since according to the invariant the n th element will then the largest. The n calls of ±IND-MIN gives the following bound on the time complexity: Θ n X i = 1 i ! = Θ ( n 2 ) This holds for both the best- and worst-case running time. 2.2 - 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/05/2011 for the course COMS 133 taught by Professor Das during the Spring '10 term at Tel Aviv Uni..

Page1 / 51

solution+to+cormen - Solutions for Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online