Draw a box trace of the function BinarySearch, Shown below.The initial call is B
ID: 3612815 • Letter: D
Question
Draw a box trace of the function BinarySearch, Shown below.The initial call is BinarySearch (A,0,9,81,Index), where the arrayA contain the values therefore the first box looks like this: value = 81 first = 0 last = 9 mid = ? value < A [mid] (or else value > A [Mid] ) Stick to this notation. static int BinarySearch (const int A[], int first, int Last,int Value) { int Index; if (First > Last) Index = -1; else { int Mid = (First + Last) / 2; if (Value == A[Mid]) Index = Mid; else if (value < A[Mid]) BinarySearch (A, First, Mid-1, Value,Index); else BinarySearch (A, Mid+1, Last, Value,Index); } return Index; } Draw a box trace of the function BinarySearch, Shown below.The initial call is BinarySearch (A,0,9,81,Index), where the arrayA contain the values therefore the first box looks like this: value = 81 first = 0 last = 9 mid = ? value < A [mid] (or else value > A [Mid] ) Stick to this notation. static int BinarySearch (const int A[], int first, int Last,int Value) { int Index; if (First > Last) Index = -1; else { int Mid = (First + Last) / 2; if (Value == A[Mid]) Index = Mid; else if (value < A[Mid]) BinarySearch (A, First, Mid-1, Value,Index); else BinarySearch (A, Mid+1, Last, Value,Index); } return Index; }Explanation / Answer
Dear... As you have not given array A in order to traceout i am considering arrayA={ 22,25,45,52,61,68,70,81,90} First pass: value =81 first=0 last=8 mid=(0+8)/2=4 A[mid]=61 A[mid]=61 value>A[mid] call to binary search Second pass: value=81 first=5 last=9 mid=7 value==A[mid] return 7 but there is a slite modification required incode static int BinarySearch (const int A[], intfirst, int Last, int Value) { int Index; if (First > Last) Index = -1; else { int Mid = (First + Last) / 2; if (Value == A[Mid]) return Mid; else if (value < A[Mid]) Index=BinarySearch (A, First,Mid-1, Value,Index); else Index=BinarySearch (A, Mid+1, Last,Value,Index); } return Index; } static int BinarySearch (const int A[], intfirst, int Last, int Value) { int Index; if (First > Last) Index = -1; else { int Mid = (First + Last) / 2; if (Value == A[Mid]) return Mid; else if (value < A[Mid]) Index=BinarySearch (A, First,Mid-1, Value,Index); else Index=BinarySearch (A, Mid+1, Last,Value,Index); } return Index; } Hope this will helpyou....
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.