oldsolution

# oldsolution - Solutions for Introduction to algorithms...

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

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

View Full Document

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.

Unformatted text preview: Solutions for Introduction to algorithms Philip Bille Spring 2001 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@diku.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. Since a second edition for the book recently has been done this document is longer being updated. Have fun with your algorithms. Best regards, Philip Bille Last update: may 17, 2001 1.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. 1.1- 3 Algorithm 1 L INEAR-S EARCH ( 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 1.2- 1 This is a slight improvement of the requested algorithm: sorting is done in-place instead of using a second array. 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 = h a 1 , a 2 , . . . a n i Output: sorted A . for i 1 to n do j F IND-M IN ( A, i, n ) A [ j ] A [ i ] end for The n calls of F IND-M IN 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. 1.2- 2 Given that each element is equally likely to be the one searched for, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half. Both the worst-case and average-case of L INEAR-S EARCH is ( n ) . 1.2- 3 Solve the Element Uniqueness Problem in ( n lg n ) time. Simply sort the numbers and perform a linear run through the array searching for duplicates. 1.2- 5 n 3 /1000- 100n 2- 100n + 3 = ( n 3 ) . 2 1.2- 6 One can modify an algorithm to have a best-case running time by specializing it to handle a best- case input efficiently. 1.3- 2 Algorithm 3 M ERGE ( A, p, q, r ) Input: Two sorted subarrays A [ p . . . q ] , A [ q + 1 . . . r ] Output: sorted A [ p . . . r ] . if p > r then return end if i p j q + 1 while i 6 q and j 6 r do if A [ i ] 6 A [ j ] then B [ i + j- 1 ] A [ i ] i i + 1 else B [ i + j- 1 ] A [ j ] j j + 1 end if end while A B 1.3- 5 A recursive version of binary search on an array. Clearly, the worst-case running time is ( lg n ) ....
View Full Document

## This note was uploaded on 04/03/2008 for the course COMP 101 taught by Professor Wolmer during the Spring '08 term at Wichita State.

### Page1 / 41

oldsolution - Solutions for Introduction to algorithms...

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

View Full Document
Ask a homework question - tutors are online