Question

# 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.1
Binary 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 indexed
from 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 gt; high )
return 0;
else {
mid = [( low + high )/2] ;
if (x == S[mid])
return mid
else if (x lt; S[mid])
return location (low, mid – 1);
else
return location (mid + 1, high );Engineering Technology