{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cp11_12_ps10_sol

# cp11_12_ps10_sol - Home Work 10 The problems in this...

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

Home Work 10 The problems in this problem set cover lectures C1 1 and C1 2 1. a. Define a recursive binary search algorithm. If lb > ub Return -1 else Mid := (lb+ub)/2 If Array(Mid) = element Return Mid Elsif Array(Mid) < Element Return Binary_Search(Array, mid+1, ub, Element) Else Return Binary_Search(Array, lb, mid-1, Element) End if End if b. Implement your algorithm as an Ada95 program. 46 . function Binary_Search (My_Search_Array : My_Array; Lb : Integer; Ub: Integer; Element : Integer) return Integer is 47 . mid : integer; 48 . begin 49 . if (Lb> Ub) then 50 . return - 1 ; 51 . else 52 . Mid := (Ub+Lb)/ 2 ; 53 . if My_Search_Array(Mid) = Element then 54 . return (Mid); 55 . elsif My_Search_Array(Mid) < Element then 56 . return (Binary_Search(My_Search_Array, Mid+ 1 , Ub, Element)); 57 . else 58 . return (Binary_Search(My_Search_Array, Lb, Mid- 1 , Element)); 59 . end if ; 60 . end if ; 61 . 62 . end Binary_Search; 63 . end Recursive_Binary_Search;

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

View Full Document
c. What is the recurrence equation that represents the computation time of your algorithm?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}