Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C & Secure https/www.mis.nuigalway le/papers,public/2014 2015/MA ST AM/2014 2015

ID: 3717907 • Letter: C

Question

C & Secure https/www.mis.nuigalway le/papers,public/2014 2015/MA ST AM/2014 2015, CS209 1.12.PDF Yube Oulook.com squer ULTIMATE GUITAR T Google Maps M Inbox (2)-squeegye Open24-Online BarDownload music, mo A Fxrbook (o) Use the Euclidean algorithm (showing your intermediate stepe) to calculate the great est common divisor of 163 and 59. If you implemented this using a recursive function gca(x,y), how many calls to the function are needed to calculate ged (163,59)? (b) Suppose we carry out a linear (sequential) search of an unordered list L with n items. We are looking for an object X which occurs exactly once in the list, and we start our search from the left of the list. The probability that the object is at any given position is not uniform: Rather it is given by the function 2i Prob.(X is at position i) -n Determine the average number of comparisons we must make to find X. (In your analysis here, you may need to make use of the fact that the sums of the squares of the first n natural uumbers is nin +1)(2n + 1)/6.) (c) If one were to carry out the linear search described in (b) in reverse order (ie. starting from the right). deternine again the average number of comparisons made

Explanation / Answer

answer a)

number of call to the function to calculate(163,59) is 7 calls including the intial function call from main else it is 6 calls without the intial call.

answer c)

In a best case, we can found out target element in just 1 comparison. In the worst case, we might go for n comparisons, since we doing a successful search so at max we will able to find out target element at index n-1 if we start array indexing from 0. So average number of comparisons in case of successful search = (n +1 )/2