Question: For Task B, you need to implement an algorithm for nding: 0 a given value 0 within an array sorted by value from lowest to

For Task B, you need to implement an algorithm for nding: 0 a given value 0 within an array sorted by value from lowest to highest (or in alphabetical order for strings, or any other order for other types that can be compared to each other). 0 Binary search rst looks at the middle element, then determines if the value that is being searched for, is a. in the upper half or b. the lower half of the array. 0 The algorithm then continues "splitting" the appropriate subarray in half 0 until the desired value is found, if it exists. Here is a pseudo code description for the binary search algorithm: 1 // return the position of an element with value k 2 // in the given sorted array A 3 // 4 // if k is not in A, return A.length 5 6 int binary(A, k) { 7 l = l; 8 r = A.1ength; // l and r are "outside bounds" of the array 9 while (1+1 != r) { // Stop when l and r meet. 10 i = (l+r)/2; // Check middle of remaining subarray 11 if (k == A[i]) return i; // Found it! 12 else if (k
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
