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
A matlab function to calculate filter order

IS Functions for Strings: There are many functions for strings, that return logical true or false. The function isletter returns the logical true when the character is a lette

Vector operations: As vectors are special cases of matrices, the matrix operations elaborated (addition, subtraction, multiplication, scalar multiplication, transpose) work on

ischar function: The ischar function return the logical true if an array is a character array, or logical false if not. >> vec = 'EK127'; >> ischar(vec) ans =

Finding products by for loop: an illustration, when 5 is passed to be the value of the input argument n, the function will compute and return 1 + 2 + 3 + 4 + 5, or 15: >> s

Finding sums and products: A very general application of a for loop is to compute sums and products. For illustration, rather than of just printing the integers 1 through 5, w

Function rmfield - structure: The function rmfield eliminates a field from the structure. It returns a new structure with field eliminated, but does not modify the original st

Passing arguments to functions: In all these functions examples faraway, at least one of the arguments was passed in the function call to be the value(s) of the equivalent inp

Built-in colormaps: The MATLAB has numerous built-in colormaps which are named; the reference page on colormap shows them. Calling the function colormap without passing any ar

Example of Exponential function modular program: In order to view the distinction in the approximate value for e as n increases, the user kept choosing Limit & entering larger