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
Illustration of Graphics properties: A particular property can also be exhibited, for illustration, to view the line width:     >> get(hl,'LineWidth')     ans =

Scaling:   change a row by multiplying it by a non-zero scalar sri →  ri For illustration, for the matrix:

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

Matrix definitions: As we know the matrix can be thought of as a table of values in which there are both rows and columns. The most common form of a matrix A (that is sometime

Patch function - graphics objects: The patch function is used to generate a patch graphics object, which is made from 2-dimensional polygons. The patch is defined by its verti

Illustration of finding a sting: Let's enlarge this, and write a script which creates a vector of strings which are phrases. The outcome is not suppressed so that the string

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric

readlenwid function: function call: [length, width] = readlenwid; function header: function [l,w] = readlenwid In the function call, not any argument is passed; henc

Illustration of Matrix solutions: For illustration, consider the three equations below with 3unknowns x 1 ,x 2 , and x 3 : We can write this in the form Ax = b here A

Application: Menu driven Modular Program Numerous longer, more involved programs which have interaction with the user are menu-driven, that means that the program prints a men