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
Solving 2 × 2 systems of equations: However this may be easy in a MATLAB, in normal finding solutions to the systems of equations is not. The systems which are 2 × 2 are, thou

IS Functions in Matlab: There are many functions which are built into MATLAB which test whether or not something is true; these function names start with the word is. As these

Example of file ploting data: As the other example, a data file called 'compsales.dat' stores the sales figures (in millions) for divisions in a company. Each line in the f

Example of image processing: The other illustration generates a 5 × 5 matrix of arbitrary integers in the range from 1 to the number of colors; the resultant image is as shown

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

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

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

Illustration of initializing the data structure: illustration of initializing the data structure by preallocating is here as shown: >> cyls(3) = struct('code', 'c', 'dimen

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

Algorithm for appex subfunction: The algorithm for appex subfunction is as shown:  Receives x & n as the input arguments.  Initializes a variable for running sum of t