Function used in binary search, MATLAB in Engineering

Assignment Help:

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


Related Discussions:- Function used in binary search

Function cellplot - cell array, Function cellplot - Cell array: The fu...

Function cellplot - Cell array: The function cellplot place a graphical display of the cell array in a figure Window; though, it is a high-level view and fundamentally just di

Use of built-in colormaps - image processing, Use of built-in colormaps: ...

Use of built-in colormaps: MATLAB has built-in colormaps, it is also possible to generate others by using combinations of any colors. For illustration, the following generates

Gauss elimination, Gauss Elimination: The Gauss elimination technique ...

Gauss Elimination: The Gauss elimination technique consists of:    Generating the augmented matrix [A b]    Applying EROs to augmented matrix to obtain an upper trian

Illustration of variable scope, Illustration of Variable scope: Runnin...

Illustration of Variable scope: Running this function does not add any of variables to the workspace, as elaborated: >> clear >> who >> disp(mysum([5 9 1]))

Execute a exponential function program, Execute a exponential function prog...

Execute a exponential function program: Running the script will take up the menu as shown in the figure: Then, what happens will totally depend on which button(s) the

Matrix definitions, Matrix definitions: As we know the matrix can be t...

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

Vectors of structures, Vectors of Structures: In numerous applications...

Vectors of Structures: In numerous applications, involving database applications, information generally would be stored in the vector of structures, instead of in individual s

Technique is to create one element - vector, Technique is to create one ele...

Technique is to create one element - vector: Technique is to create one element with the values from one structure, and use repmat to replicate it to the preferred size. Then,

Use of nested if-else statements, Use of Nested if-else statements: By...

Use of Nested if-else statements: By using the nested if-else to select from among the three possibilities, not all the conditions should be tested. In this situation, if x is

Function used in binary search, Function used in binary search: The fu...

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 func

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd