Engineering Homework Help
I was told to rewrite the algorithm below to a standard algorithm. I am not sure if I need an exit statement or
I was told to rewrite the algorithm below to a standard algorithm. I am not sure if I need an exit statement or
not.
Here is my algorithm and the image down below is the algorithm that I needed to rewrite:
Function location (S, n, x, low, high)
// S is an integer type that represented the sorted (nondecreasing order) input array of n integers //
// n is an integer type that represented the input size of an array //
// x is an integer type that represented the input key value //
//low is an integer type that represented the minimum value in the array //
//high is an integer type represented the maximum value in the array //
Integer S (1 : n), n, x, low, high, mid, index
// index is an integer type represented the return value //
if (low > high) then
index ← 0
else
mid = (low + high) / 2
if (x = S[mid]) then
index ← mid
else
if (x < S[mid]) then
ndex ← location (low, mid – 1)
else
index ← location (mid + 1, high)
return (index)
exit #### Do I need the exit here or just take it off? ####
end location
Algorithm 2.1Binary Search (Recursive)Problem: Determine whether x is in the sorted array S of size n.Inputs: positive integer n, sorted (nondecreasing order) array of keys S indexedfrom 1 to n, a key x.Outputs: location, the location of x in S (0 if x is not in S).index location (index low, index high)index mid;if ( low > high )return 0;else {mid = [( low + high )/2] ;if (x == S[mid])return midelse if (x < S[mid])return location (low, mid – 1);elsereturn location (mid + 1, high );