<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Binary-Search on BoilTask's Blog</title><link>https://boiltask.com/en/tags/binary-search/</link><description>Recent content in Binary-Search on BoilTask's Blog</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Tue, 26 Jul 2016 17:24:57 +0800</lastBuildDate><atom:link href="https://boiltask.com/en/tags/binary-search/index.xml" rel="self" type="application/rss+xml"/><item><title>Binary Search — Thinking by Comparing with the Answer</title><link>https://boiltask.com/en/knowledge/binary-search/</link><pubDate>Tue, 26 Jul 2016 17:24:57 +0800</pubDate><guid>https://boiltask.com/en/knowledge/binary-search/</guid><description>&lt;p&gt;Binary search is not so much an algorithm
as a way of thinking.&lt;/p&gt;
&lt;h2 id="core-code"&gt;&lt;a href="#core-code" class="header-anchor"&gt;&lt;/a&gt;Core code
&lt;/h2&gt;
&lt;div class="highlight-wrapper"&gt;
&lt;div class="highlight-header"&gt;
&lt;span class="highlight-language"&gt;cpp&lt;/span&gt;
&lt;/div&gt;
&lt;div class="highlight-body"&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;judge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="advantages"&gt;&lt;a href="#advantages" class="header-anchor"&gt;&lt;/a&gt;Advantages
&lt;/h2&gt;&lt;p&gt;It brings an ordinary search down from O(n) to O(log n).&lt;/p&gt;
&lt;h2 id="requirements"&gt;&lt;a href="#requirements" class="header-anchor"&gt;&lt;/a&gt;Requirements
&lt;/h2&gt;&lt;p&gt;The data you search must be sorted.&lt;/p&gt;
&lt;h2 id="explanation"&gt;&lt;a href="#explanation" class="header-anchor"&gt;&lt;/a&gt;Explanation
&lt;/h2&gt;&lt;p&gt;Binary search shrinks the range in which the answer can lie, again and again, until the optimum is found. In coursework and in contests you will often meet problems that combine binary thinking with some other algorithm.&lt;/p&gt;
&lt;h2 id="uses"&gt;&lt;a href="#uses" class="header-anchor"&gt;&lt;/a&gt;Uses
&lt;/h2&gt;&lt;h3 id="finding-a-value-in-a-sorted-array"&gt;&lt;a href="#finding-a-value-in-a-sorted-array" class="header-anchor"&gt;&lt;/a&gt;Finding a value in a sorted array
&lt;/h3&gt;&lt;p&gt;Binary search can answer, for a given array,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;whether a value exists&lt;/li&gt;
&lt;li&gt;where that value sits&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="solving-an-equation-within-a-tolerance"&gt;&lt;a href="#solving-an-equation-within-a-tolerance" class="header-anchor"&gt;&lt;/a&gt;Solving an equation within a tolerance
&lt;/h3&gt;&lt;p&gt;Halve the interval over and over until the solution is precise enough.&lt;/p&gt;
&lt;h3 id="maximising-the-minimum"&gt;&lt;a href="#maximising-the-minimum" class="header-anchor"&gt;&lt;/a&gt;Maximising the minimum
&lt;/h3&gt;&lt;h3 id="minimising-the-average"&gt;&lt;a href="#minimising-the-average" class="header-anchor"&gt;&lt;/a&gt;Minimising the average
&lt;/h3&gt;&lt;p&gt;Problems of this shape are easy to compare
because you can tell whether a candidate is above or below the real answer.
So even without knowing what the answer is,
you can shrink its range by comparison.&lt;/p&gt;
&lt;p&gt;Reference:&lt;br&gt;
&lt;em&gt;Programming Contest Challenge Book&lt;/em&gt;&lt;br&gt;
Baidu Baike&lt;/p&gt;</description></item></channel></rss>