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
Program of passing arguments to functions: This was an illustration of a function which did not receive any input arguments nor did it return any output arguments; it easily a

Symbolic Variables and expressions: The MATLAB has a type known as sym for the symbolic variables and expressions; these work with strings. The illustration, to generate a sym

Set Operations: The MATLAB has numerous built-in functions which perform set operations on vectors. These involve intersect, union, setdiff, unique, and setxor. All these func

Reading from a File in a While Loop: Though in most languages the combination of a loop and an if statement would be essential to determine whether or not the elements in a ve

Sound Files: The sound signal is an illustration of a continuous signal which is sampled to result in a discrete signal. In this situation, sound waves traveling through the a

Finding a sting - function strfind: The function strfind does necessarily similar thing, except that the order of the arguments does make dissimilarity. The common form is str

Plotting File data: It is frequently essential to read data from a file and plot it. Generally, this entails knowing the format of the file. For illustration, let us suppose t

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

Passing Structures to Functions: The whole structure can be passed to a function, or separate fields can be passed. For illustration, here are the two distinct versions of a f

Creating the structure Variables: Creating a structure variable can be accomplished by simply storing the values in fields by using assignment statements, or by using the stru