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
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

Q.1: Consider the transmission of a sinusoid x(t) = cos(2f0t) through a channel a ected by multipath and Doppler. Let there be two paths, and assume the sinusoid is being sent fro

Finding a sting - function strfind: The function strfind does necessarily similar thing, except that the order of the arguments does make dissimilarity. The common form is str

Replacing a string - function strrep: The function strrep finds all the occurrences of a substring within the string, and substitutes them with a new substring. The order of a

Finding a sting - function findstr: The function findstr receives two strings as input arguments. It finds all the occurrences of shorter string contained by the longer, and r

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

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

Reading from a File in a While Loop: Though in most languages the combination of a loop and an if statement would be essential to determine whether or not the elements in a ve

Program to Counting in a while loop: The script initializes variable counter to 0. Then, in the while loop action, each and every time the user successfully enter a number, th