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

[2 3 81 100 120 130 140 150 160 170] Draw a box trace of the function BinarySear

ID: 3612820 • Letter: #

Question

[2 3 81 100 120 130 140 150 160 170] 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 array
A={ 2 3 81 100 120 130 140 150 160 170} First pass: value =81                 first=0                  last=9                 mid=(0+9)/2=4                  value<A[mid]                          call to binary search Second pass: value=81                      first=0                      last=3                      mid=1                      value>list[mid]                                 call to binary search Third pass : value=81                   first=2                   last=3                   mid=2                   value==list[mid]                          return mid 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; } As it recursive function the recursive calls returns value infunction returns value so values must be assigned As it recursive function the recursive calls returns value infunction returns value so values must be assigned Hope this will helpyou....
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote