Binary search, MATLAB in Engineering

Binary Search:

The binary search supposes that the vector has been sorted first. The algorithm is just similar to the way it works whenever looking for a name in a phone directory (that is sorted in alphabetic order). To find the value of a key, look at the element in the middle.

  • If that is the key, then the index has been found.
  • When it is not the key, decide whether to search the elements before or after this place and adjust the range of values in which the search occurs and begin this process again.

To implement this, we will use variables low and high to identify the range of values in which to search. To start, the value of low will be 1, and the value of high will become the length of the vector. The variable mid be the index of the element in the middle of the range from low to high. When the key is not found at mid, then there are two possible way to adjust the range. When the key is less than the value at mid, we change high to mid - 1. When the key is greater than the value at mid, then we change low to mid 1.

 

Posted Date: 10/22/2012 7:58:38 AM | Location : United States







Related Discussions:- Binary search, Assignment Help, Ask Question on Binary search, Get Answer, Expert's Help, Binary search Discussions

Write discussion on Binary search
Your posts are moderated
Related Questions
Example of Gauss-jordan: For a 2×2 system, this would results and for a 3 × 3 system, Note that the resulting diagonal form does not involve the right-most col

deblank function: The deblank function eliminates only trailing blanks from the string, not leading the blanks. The strtrim function will eliminate both the leading and traili

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 to change the line width from the default: For illustration, to change the line width from the default of 0.5 to 1.5: >> set(hl,'LineWidth',1.5) As long as the

Cross Product: The cross or outer product a × b of two vectors a and b is defined only whenever both a and b are the vectors in three-dimensional space, that means that they b

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

Illustration of finding a sting: Let's enlarge this, and write a script which creates a vector of strings which are phrases. The outcome is not suppressed so that the string

Interchange rows : for illustration interchanging rows ri and rj is written as

Illustration of Passing arguments to functions: Here is an illustration of calling this function: >> printrand() The random # is 0.94 As nothing is passed to

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