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
readlenwid function: function call: [length, width] = readlenwid; function header: function [l,w] = readlenwid In the function call, not any argument is passed; henc

Use polyval to evaluate the derivative at xder. This will be the % slope of the tangent line, "a" (general form of a line: y = ax + b). % 4. Calculate the intercept, b, of t

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

Use of While loop: Here is an illustration of calling the function, passing 5000 for the value of the input argument high. >> factgthigh(5000) ans = 5040 The itera

Function strncmp: The function strncmp compares only the first n characters in the strings and ignores the rest. The initial two arguments are strings to compare, and third ar

Displaying the cell arrays: There are several techniques of displaying the cell arrays. The celldisp function shows all elements of the cell array:   >> celldisp(cellro

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

Changing Case: The MATLAB has two functions which convert strings to all uppercase letters, or all lowercase, known as the upper and lower. >> mystring = 'AbCDEfgh';

Sound Files: The sound signal is an illustration of a continuous signal which is sampled to result in a discrete signal. In this situation, sound waves traveling through the a

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