Implementation of binary search, MATLAB in Engineering

Implementation of binary search:

The binary search can be implemented as a recursive function. The recursive function below also implements this binary search algorithm. It receives four arguments: a sorted vector, and a key to search for, and the values of low and high (which, to start, will be 1 and the length of the vector). It will return -1 when the key is not in the vector, or the index of element in which it is found. The base cases in the algorithm are if low > high, that means the key is not in the vector, or whenever it is found. Or else, the common case I to adjust the range and call the binary search function again.

1764_Implementation of binary search.png

The illustrations of calling this function is shown here:

>> recbinsearch(svec, 5,1,length(svec))

ans =

    3

>> recbinsearch(svec, 25,1,length(svec))

ans =

    7

>> recbinsearch(svec, 4,1,length(svec))

ans =

    -1

Posted Date: 10/22/2012 8:04:36 AM | Location : United States







Related Discussions:- Implementation of binary search, Assignment Help, Ask Question on Implementation of binary search, Get Answer, Expert's Help, Implementation of binary search Discussions

Write discussion on Implementation of binary search
Your posts are moderated
Related Questions
Finding a sting - function findstr: The function findstr receives two strings as input arguments. It finds all the occurrences of shorter string contained by the longer, and r

I have a vector of X, one for Y , one for x-direction velocity U and one for y-direction velocity V. they are at same size. How can I plot streamline of that flow? I follow all exa

Graphics Properties: The MATLAB uses the Handle Graphics in all its figures. All figures consist of various objects, each of which is assigned a handle. The object handle is a

Structures: The Structures are data structures which group together values which are logically related in what are known as the fields of structure. The benefit of structures

Print from the structure: To print from the structure, a disp function will show either the whole structure or a field. >> disp(package) item_no: 123 cost: 19.99

deblank function: The deblank function eliminates only trailing blanks from the string, not leading the blanks. The strtrim function will eliminate both the leading and traili

Illustration of Gauss elimination: For illustration, for a 2 × 2 system, an augmented matrix be: Then, the EROs is applied to obtain the augmented matrix into an upper

Subfunctions: Though, it is possible to have more than one function in a given M-file. For illustration, if one function calls the other, the first function would be the prima

Removing Whitespace Characters: The MATLAB has functions which will eliminate trailing blanks from the end of a string and/or leading blanks from the starting of a string.

Reading from a Mat-File: The load function is used to read from various types of files. As with save function, by default the file will be supposed to be a MAT-file, and load