cp11_12_ps10_sol

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

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

Page1 / 4

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

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

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