Binary search

In computer science, Binary search is a search algorithm for searching a set of sorted data for a particular data. In its most simple form, binary search assumes the data is sorted and takes advantage of that characteristic. As the data has to be sorted in the first place, it cannot be applied to data such as compound structure unless the programmer writes a special method to compare structures. However, not all languages support this ability to automatically utilise programmer created functions.

Binary search is often used together with other algorithms such as insertion sort or data structures.

Many of standard libraries of languages provide a way to do binary search. C++'s STL (Standard Template Library) provides algorithm functions lower_bound and upper_bound. Java offers binarySearch methods in Arrays class.

Binary search is a logarithmic algorithm and executes in O(log n). Specifically, 1 + log2 N iterations are needed to return an answer. It is a considerably faster than a linear search. It can be implemented using recursion or iteration, although in many languages it is more elegantly expressed recursively.

This sample Ruby implementation returns true if at least one of the elements of array is equal to value, otherwise return false

def binary_search(array,value)
   first=0
   last=array.size - 1
   while (first <= last)
       mid = (first + last) / 2
       if (value > array[mid])
           first = mid + 1
       elsif (value < array[mid])
           last = mid - 1
       else
          return true
       end
   end
   return false
end

An example of binary search in action is a simple guessing game in which a player has to guess a positive integer selected by another player between 1 and N, using only questions answered with yes or no. Supposing N is 16 and the number 11 is selected, the game might proceed as follows.

Is the number greater than 8? (Yes)
Is the number greater than 12? (No)
Is the number greater than 10? (Yes)
Is the number greater than 11? (No)

Therefore, the number must be 11. At most ceiling(log2 N) questions are required to determine the number, since each question halves the search space. Note that one less question (iteration) is required than for the general algorithm, since the number is constrained to a particular range.

If N is unknown or infinite, we can still find the mysterious number k in at most steps by first computing an N which is larger than or equal to k:

def find_N(array,value)
   N=1
   while (value>array[N-1])
       N=N*2
   end
   return N
end

In the guessing game when we don't know the value of N, the game would be:

Is the number greater than 1? (Yes)
Is the number greater than 2? (Yes)
Is the number greater than 4? (Yes)
Is the number greater than 8? (Yes)
Is the number greater than 16? (No, N=16, proceed as above)
Is the number greater than 12? (No)
Is the number greater than 10? (Yes)
Is the number greater than 11? (No)

Also observe that we don't need to ask about 8 twice.

In Wikipedia, it is possible to use a binary search to see which user added content to an article. One can find a revision which does not include the content, and do a binary search through the history to see where it appeared. Due to the asymptotic analysis above, this is far quicker than checking every difference.



In the News

Is Your Trailing Spouse a Significant Other?
Today’s blog post is a little bit of a cheat, it’s a re-run of a feature I wrote for HMSBeagle the now defunct life sciences webzine for its Adapt or Die column to which I contributed on a monthly basis for a couple of years. However, the content and sentiment (if not necessarily the cited [...]

Chemical In Brain Acts Like A Fuel Gauge
The neurotransmitter norepinephrine can alert the brain to dangerously low blood sugar levels, according to a new study. Finding has implications for diabetes research.

Old Discovery Could Boost Ethanol Production From Plant Fiber
A discovery some 40 years ago is showing promise as a chemical pre-treatment that breaks down plant fiber. That could release the simple sugars in corn stalks or switchgrass so they can be fermented into ethanol. And that could add value to Iowa's crops or the fibrous co-products of ethanol production.

[Odd] A Romanian couple has named their son Yahoo as a sign of gratitu
Daily Libertatea said on Thursday Cornelia and Nonu Dragoman, both from Transylvania, met and decided they were meant for each other following a three-month relationship over the net.They married and had a baby this Christmas, whom they decided to name after one of the worldwide web's most popular portals."We named him Lucian Yahoo after my father and the net, the main beacon of my life,"Cornelia Dragoman was quoted as saying.

Activities Of Subseafloor Life More Diverse Than Expected
Research on cores recovered by the Ocean Drilling Program show that the activity of microbial life beneath the seafloor is far more diverse than expected, scientists report in the December 24 issue of Science.

Darwin Correspondence Project
This website presents around 5,000 letters written by and to naturalist Charles Darwin, providing "information about his intellectual development, Victorian science and society. They [throw] light on his formative years and the voyage of the Beagle, on the period which led up to the publication of 'The Origin of Species' and the subsequent heated debates."Includes a section on Darwin and religion. From the University of Cambridge Library.

Kyoto Nations Explore Baby Steps
Talks next week in Nairobi look for agreement between 189 countries to extend the Kyoto pact on global warming and decisions on how to move forward. Scientists say it'll take much tougher caps to avert catastrophic weather changes.

Gene Changes Linked To Deficient Immune Suppression In Multiple Sclero
Oregon Health &Science University researchers have found that multiple sclerosis patients have lower expression of the FOXP3 gene found in a subset of T-cells that may regulate defense against MS and other autoimmune diseases. They say when FOXP3 is reduced due to abnormalities in expression, the suppressive activity of regulatory T-cells also plummets. But in a separate study, a T-cell receptor peptide vaccine called NeuroVax was shown to increase FOXP3 expression among MS patients.

1906 Earthquake Centennial Alliance
The objective of this organization is to "use the centennial of the 1906 earthquake [in the San Francisco Bay Area] to highlight a century of progress in understanding earthquake hazards and reducing risks as well as to commemorate the cultural and social response to this historic event."The site features a speakers bureau, and information about activities, events, and exhibitions. Also includes links to related information.

[Odd] A Romanian couple has named their son Yahoo as a sign of gratitu
Daily Libertatea said on Thursday Cornelia and Nonu Dragoman, both from Transylvania, met and decided they were meant for each other following a three-month relationship over the net.They married and had a baby this Christmas, whom they decided to name after one of the worldwide web's most popular portals."We named him Lucian Yahoo after my father and the net, the main beacon of my life,"Cornelia Dragoman was quoted as saying.


MP3 Music Downloads

Preview songs, Download Free Music,Burn CDs at ITunes.com
iTunes_RGB_9mm

 


Google




InformationQuickFind.com - Find Information Fast

Links