{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

solution+to+cormen

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

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

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 [email protected] . 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

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

View Full Document
1.2 - 2 Insertion sort beats merge sort when 8n 2 < 64n lg n , n < 8 lg n , 2 n/8 < n . This is true for 2 n 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 · 10 7 2 36 · 10 8 2 864 · 10 8 2 2592 · 10 9 2 94608 · 10 10 2 94608 · 10 12 n 10 12 36 · 10 14 1296 · 10 16 746496 · 10 16 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 10 94608 · 10 12 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
2.1 - 2 In line 5 of I NSERTION -S ORT alter A [ i ] > key to A [ i ] < key in order to sort the elements in nonincreasing order. 2.1 - 3 Algorithm 1 L INEAR -S EARCH ( A, v ) Input: A = a 1 , a 2 , . . . a n and a value v . Output: An index i such that v = A [ i ] or nil if v 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 fullfilled by this loop invariant. 2.2 - 1 n 3 /1000 - 100n 2 - 100n + 3 = Θ ( n 3 ) . 2.2 - 2 Assume that F IND -M IN ( 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 S ELECTION -S ORT ( A ) Input: A = a 1 , a 2 , . . . a n Output: sorted A . for i 1 to n - 1 do j F IND -M IN ( A, i, n ) A [ j ] A [ i ] end for As a loop invariant we choose that A [ 1, . . . , i - 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 F IND -M IN gives the following bound on the time complexity: Θ n 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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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
Ask a homework question - tutors are online