Function used in binary search, MATLAB in Engineering

Function used in binary search:

The function below implements this binary search algorithm. It receives two arguments: the sorted vector and a key (on the other hand, the function could sort the vector). The value of low and high are initialized to the first and last indices in the vector. The outind that is the output argument is initialized to -1 that is the value which the function will return when the key is not found. It loops until either low is greater than high, or til the key is found.

345_Function used in binary search.png

The illustrations of calling this function are as shown here:

>> vec = randint(1,7, [1 30])

vec =

2 11 25 1 5  7  6

>> svec = sort(vec)

svec =

1 2  5 6 7 11 25

>> binsearch(svec, 4)

ans =

    -1

>> binsearch(svec, 25)

ans =

    7

>> binsearch(svec, 5)

ans =

    3

Posted Date: 10/22/2012 8:03:05 AM | Location : United States







Related Discussions:- Function used in binary search, Assignment Help, Ask Question on Function used in binary search, Get Answer, Expert's Help, Function used in binary search Discussions

Write discussion on Function used in binary search
Your posts are moderated
Related Questions
Gauss Elimination: The Gauss elimination technique consists of:    Generating the augmented matrix [A b]    Applying EROs to augmented matrix to obtain an upper trian

Interpolation and extrapolation: In most cases, it is desired to estimate values other than at the sampled data points. For illustration, we may want to estimate what the temp

Simplification Functions: There are numerous functions which work with expressions, and simplify the terms. Not all the expressions can be simplified, but the simplify functio

Image Processing: The Images are represented as grids, or matrices, of picture elements (known as pixels). In MATLAB an image usually is represented as a matrix in which each

Sorting Vectors of structures: Whenever working with vector of structures, it is very common to sort based on a particular field within the structures. For illustration, recal

Calling of Function polyval: The curve does not appear very smooth on this plot, but that is as there are only five points in the x vector. To estimate the temperature

Illustration of Preallocating a Vector: Illustration of calling the function: >> myveccumsum([5 9 4]) ans =     5  14  18 At the first time in the loop, outvec wil

Illustration of symbolic variable: When, on the other hand, z is a symbolic variable to start with, quotes are not required around the expression, and the words are automatica

Forward elimination: In forward elimination, we want to obtain a 0 in the a 21 position. To accomplish this, we can alter the second line in the matrix by subtracting from it

Comparing strings: There are few functions which compare strings and return logical true when they are equivalent or logical false when not. The function strcmp compares the s