<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Binary_search</id>
		<title>Binary search - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Binary_search"/>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;action=history"/>
		<updated>2026-04-24T05:31:27Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=2034&amp;oldid=prev</id>
		<title>162.158.46.6: Changed last letter from 'm' to 'r' in the case 1 of proof of correctness.</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=2034&amp;oldid=prev"/>
				<updated>2017-03-19T05:17:40Z</updated>
		
		<summary type="html">&lt;p&gt;Changed last letter from &amp;#039;m&amp;#039; to &amp;#039;r&amp;#039; in the case 1 of proof of correctness.&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 05:17, 19 March 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L71&quot; &gt;Line 71:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 71:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The index we are seeking is at most &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The index we are seeking is at most &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This is certainly true immediately before the first iteration of the loop, where &amp;lt;code&amp;gt;l = 0&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r = n&amp;lt;/code&amp;gt;. Now we claim that if it is true before a given iteration of the loop, it will be true after that iteration, which, by induction, implies our claim. First, the value &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; will be exactly the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, if their sum is even, or it will be 0.5 less than the mean, if their sum is odd, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is computed as the arithmetic mean rounded down. This implies that, in any given iteration of the loop, &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;'s initial value will be at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, and strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;. Now:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This is certainly true immediately before the first iteration of the loop, where &amp;lt;code&amp;gt;l = 0&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r = n&amp;lt;/code&amp;gt;. Now we claim that if it is true before a given iteration of the loop, it will be true after that iteration, which, by induction, implies our claim. First, the value &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; will be exactly the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, if their sum is even, or it will be 0.5 less than the mean, if their sum is odd, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is computed as the arithmetic mean rounded down. This implies that, in any given iteration of the loop, &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;'s initial value will be at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, and strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;. Now:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Case 1'': &amp;lt;code&amp;gt;A[m] &amp;amp;lt; c&amp;lt;/code&amp;gt;. This means that no element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, can be greater than &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;, because the array is sorted. But because we know that such an element exists (or that it is the hypothetical element just past the end of the array) in the current range delineated by indices &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, we conclude that it must be in the second half of the range with indices from &amp;lt;code&amp;gt;m+1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive. In this case we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt; and at the end of the loop the invariant is again satisfied. This is guaranteed to increase the value of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out with value at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;. It also does not increase &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; beyond &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is initially strictly less than &amp;lt;code&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;m&lt;/del&gt;&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Case 1'': &amp;lt;code&amp;gt;A[m] &amp;amp;lt; c&amp;lt;/code&amp;gt;. This means that no element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, can be greater than &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;, because the array is sorted. But because we know that such an element exists (or that it is the hypothetical element just past the end of the array) in the current range delineated by indices &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, we conclude that it must be in the second half of the range with indices from &amp;lt;code&amp;gt;m+1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive. In this case we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt; and at the end of the loop the invariant is again satisfied. This is guaranteed to increase the value of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out with value at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;. It also does not increase &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; beyond &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is initially strictly less than &amp;lt;code&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;r&lt;/ins&gt;&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Case 2'': &amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;. This means that there exists an element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, which is at least &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;. This implies that the first such element is somewhere in this half of the range, because the array is sorted. Therefore, after setting &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, the range of indices between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive, will still contain the index we are seeking, and hence the invariant is again satisfied. This is guaranteed to decrease the value of &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;; it also will not decrease it beyond &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out at least as large as &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Case 2'': &amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;. This means that there exists an element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, which is at least &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;. This implies that the first such element is somewhere in this half of the range, because the array is sorted. Therefore, after setting &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, the range of indices between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive, will still contain the index we are seeking, and hence the invariant is again satisfied. This is guaranteed to decrease the value of &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;; it also will not decrease it beyond &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out at least as large as &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Since each iteration of the loop either increases &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; or decreases &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, the loop will eventually terminate with &amp;lt;code&amp;gt;l = r&amp;lt;/code&amp;gt;. Since the invariant still holds, this unique common value must be the solution. &amp;lt;math&amp;gt;_{\blacksquare}&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Since each iteration of the loop either increases &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; or decreases &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, the loop will eventually terminate with &amp;lt;code&amp;gt;l = r&amp;lt;/code&amp;gt;. Since the invariant still holds, this unique common value must be the solution. &amp;lt;math&amp;gt;_{\blacksquare}&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.46.6</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=2027&amp;oldid=prev</id>
		<title>162.158.46.24: /* Implementation */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=2027&amp;oldid=prev"/>
				<updated>2017-01-01T04:59:12Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Implementation&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 04:59, 1 January 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L55&quot; &gt;Line 55:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 55:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; {&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; int m = (l+r)/2;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; int m = (l+r)/2;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (A[m] &amp;lt;= c)&amp;#160; &amp;#160; &amp;#160;  /* Note use of &amp;lt; instead of &amp;lt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;=&lt;/del&gt;. */&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; if (A[m] &amp;lt;= c)&amp;#160; &amp;#160; &amp;#160;  /* Note use of &amp;lt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;= &lt;/ins&gt;instead of &amp;lt;. */&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; l = m+1;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; l = m+1;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; else&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; else&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.46.24</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1583&amp;oldid=prev</id>
		<title>Brian: removed Category:Incomplete</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1583&amp;oldid=prev"/>
				<updated>2012-02-11T23:45:27Z</updated>
		
		<summary type="html">&lt;p&gt;removed Category:Incomplete&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 23:45, 11 February 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L136&quot; &gt;Line 136:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 136:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|GLASNICI|Glasnici @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|GLASNICI|Glasnici @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|PIE|Pie @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|PIE|Pie @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Incomplete]]&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1582&amp;oldid=prev</id>
		<title>Brian at 23:44, 11 February 2012</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1582&amp;oldid=prev"/>
				<updated>2012-02-11T23:44:20Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 23:44, 11 February 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L108&quot; &gt;Line 108:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 108:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;More generally, binary search is often very useful for problems that read something like &amp;quot;find the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;P(x)&amp;lt;/math&amp;gt;&amp;quot;. For example, in {{Problem|ccc03s5|Trucking Troubles}}, the problem essentially down to finding the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that the edges of the given graph of weight at least &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; span the graph. This problem can be solved using [[Prim's algorithm]] to construct the &amp;quot;maximum spanning tree&amp;quot; (really, the minimum spanning tree using negated edge weights), but binary search provides a simpler approach. We know that &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is somewhere in between the smallest edge weight in the graph and the largest edge weight in the graph, so we start by guessing a value roughly halfway in-between. Then we use [[depth-first search]] to test whether the graph is connected using only the edges of weight greater than or equal to our guess value. If it is, then the true value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is greater than or equal to our guess; if not, then it is less. This is then repeated as many times as necessary to determine the correct value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;More generally, binary search is often very useful for problems that read something like &amp;quot;find the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;P(x)&amp;lt;/math&amp;gt;&amp;quot;. For example, in {{Problem|ccc03s5|Trucking Troubles}}, the problem essentially down to finding the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that the edges of the given graph of weight at least &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; span the graph. This problem can be solved using [[Prim's algorithm]] to construct the &amp;quot;maximum spanning tree&amp;quot; (really, the minimum spanning tree using negated edge weights), but binary search provides a simpler approach. We know that &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is somewhere in between the smallest edge weight in the graph and the largest edge weight in the graph, so we start by guessing a value roughly halfway in-between. Then we use [[depth-first search]] to test whether the graph is connected using only the edges of weight greater than or equal to our guess value. If it is, then the true value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is greater than or equal to our guess; if not, then it is less. This is then repeated as many times as necessary to determine the correct value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Binary search can also be used to find approximate solutions to equations. For example, suppose we have &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; blocks, and we wish to use as many of them as possible to construct a square-based pyramid. A square-based pyramid has an apex consisting of a single block. The next layer under it is a 2 block by 2 block square (so 4 blocks in all), then there is a layer with 9 blocks, and so on down. How many layers can we construct using the blocks given?&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;It can be shown that there a square-based pyramid with &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; levels requires &amp;lt;math&amp;gt;f(n) = \frac{1}{6}(2n^3 + 3n^2 + n)&amp;lt;/math&amp;gt; blocks. This polynomial is increasing on &amp;lt;math&amp;gt;[0, \infty)&amp;lt;/math&amp;gt;. But of course as a trivial upper bound we cannot construct more that &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; levels using &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; blocks. So, by using binary search, we can find the maximum &amp;lt;math&amp;gt;x \in [0, c)&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;\frac{1}{6}(2x^3 + 3x^2 + x) \leq c&amp;lt;/math&amp;gt;. (More efficient solutions are accessible with higher mathematics.)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Continuous==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Continuous==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;TODO&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The continuous version of the problem is like the number guessing game applied to real numbers: Alice thinks of a real number between 0 and 1, and Bob has to guess it. Bob begins by guessing 0.5, and so on. Clearly, in this case, no matter how many questions Bob asks, there will still be an infinite number of possibilities, though he can get greater and greater accuracy by asking more and more questions. (For example, after 10 questions, he might have narrowed down the range to (0.1015625, 0.1025390625)). However, in computer science, we are often satisfied if we can approximate the correct answer to arbitrary precision, rather than determine it exactly outright. (After all, the native real types of most microprocessors (see [[IEEE 754]]) can only store a fixed number of bits of precision, anyway.) Here, if, for example, Bob wants to determine Alice's number to an accuracy of one part in 10&amp;lt;sup&amp;gt;9&amp;lt;/sup&amp;gt;, then he has to ask about 30 questions, because the length of the original interval is 1, and this must be reduced by a factor of 10&amp;lt;sup&amp;gt;9&amp;lt;/sup&amp;gt;, which is about 2&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt;. In general, the time taken will be approximately &amp;lt;math&amp;gt;\log_2\frac{\ell}{\epsilon_a}&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;\ell&amp;lt;/math&amp;gt; is the length of the original interval and &amp;lt;math&amp;gt;\epsilon_a&amp;lt;/math&amp;gt; is the desired precision (expressed as absolute error).&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;More formally, we have a monotonically increasing function &amp;lt;math&amp;gt;f:A \to B&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is usually an interval of the real line and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is some totally ordered set, and we know that there is some &amp;lt;math&amp;gt;x_0 \in A&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;f(x_0) = c \in B&amp;lt;/math&amp;gt;. (Such a guarantee can be provided by the intermediate value theorem, if &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; is a continuous function.) Our objective is to find &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;|x - x_0| &amp;lt; \epsilon&amp;lt;/math&amp;gt;, that is, to approximate &amp;lt;math&amp;gt;f^{-1}(c)&amp;lt;/math&amp;gt; to some desired precision.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;This is a very general method for computing inverses of monotone functions. More specific (and faster) methods exist for specific classes of functions (such as Newton's method, the Lagrange inversion formula, or integral representations that can be computed using Gaussian quadrature); however, binary search is expedient and efficient when the precision required is not too large, and can be used for nearly all functions, because chances are they will be monotone on ''some'' interval around &amp;lt;math&amp;gt;f^{-1}(c)&amp;lt;/math&amp;gt;. The exceptions occur when the function oscillates wildly around that point (such functions are usually &amp;quot;pathological&amp;quot;, and occur only in discussion of mathematical theory and not in practice), or the function has a local extremum at &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; (here, [[ternary search]] may be used instead).&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;An example problem of a different type is {{Problem|secret|Secret Room}}. This can be solved by repeatedly guessing some radius &amp;lt;math&amp;gt;r&amp;lt;/math&amp;gt; and trying to figure out whether a circle of radius &amp;lt;math&amp;gt;r&amp;lt;/math&amp;gt; can be placed somewhere on the grid without crossing any lasers; if so, then the maximum radius is at least &amp;lt;math&amp;gt;r&amp;lt;/math&amp;gt;, and otherwise, it is less. (However, determining whether placing the circle is possible is a nontrivial problem in [[computational geometry]].)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Problems==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Problems==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L118&quot; &gt;Line 118:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 128:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc08s2p6|Landing}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc08s2p6|Landing}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc96s5|Max Distance}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc96s5|Max Distance}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|secret|Secret Room}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc03s5|Trucking Troubles}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{Problem|ccc03s5|Trucking Troubles}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|AGGRCOW|Aggressive Cows @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* {{SPOJ|AGGRCOW|Aggressive Cows @ SPOJ}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1581&amp;oldid=prev</id>
		<title>Brian at 21:36, 11 February 2012</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1581&amp;oldid=prev"/>
				<updated>2012-02-11T21:36:25Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 21:36, 11 February 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;'''Binary search''' is the term used in computer science for the discrete version of the ''bisection method'', in which given a monotone function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; over a discrete interval &amp;lt;math&amp;gt;I&amp;lt;/math&amp;gt; and a target value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; one wishes to find &amp;lt;math&amp;gt;x \in I&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;f(x) \approx c&amp;lt;/math&amp;gt;. It is most often used to locate a value in a sorted [[array]]. The term may also be used for the continuous version.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;'''Binary search''' is the term used in computer science for the discrete version of the ''bisection method'', in which given a monotone function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; over a discrete interval &amp;lt;math&amp;gt;I&amp;lt;/math&amp;gt; and a target value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; one wishes to find &amp;lt;math&amp;gt;x \in I&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;f(x) \approx c&amp;lt;/math&amp;gt;. It is most often used to locate a value in a sorted [[array]]. The term may also be used for the continuous version.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Discrete&lt;/del&gt;: &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;array&lt;/del&gt;==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Informal introduction&lt;/ins&gt;: &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the guessing game&lt;/ins&gt;==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;most commonly encountered variation of binary search&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;we &lt;/del&gt;are &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;given &lt;/del&gt;a &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;zero-indexed [[array]] &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;A&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;of size &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;that is sorted in ascending order&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;along with some value &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;c&lt;/del&gt;&amp;lt;/math&amp;gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Here &lt;/del&gt;the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;domain &lt;/del&gt;of the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;function &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;f&lt;/del&gt;&amp;lt;/math&amp;gt; is &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the set of indices into the array, and &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;f(i) = A_i&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;where &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;A_i&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is the &lt;/del&gt;element &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;at &lt;/del&gt;the &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;i&lt;/del&gt;&amp;lt;/math&amp;gt;&amp;lt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;sup&lt;/del&gt;&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;th&lt;/del&gt;&amp;lt;/&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;sup&lt;/del&gt;&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;position&lt;/del&gt;. There are two slightly different variations on what we wish to do:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;''number guessing game''&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;there &lt;/ins&gt;are &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;two players, Alice and Bob. Alice announces that she has thought of an integer between 1 and 100 (inclusive), and challenges Bob to guess it. Bob may guess any number from 1 to 100, and Alice will tell him whether his guess is correct; if it is not correct, she will tell him (truthfully) either that her number is higher than his guess, or that it is lower.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;One possible strategy that Bob can employ that guarantees he will eventually win is simply guessing every integer from 1 to 100 in turn. First Bob says &amp;quot;1&amp;quot; and (for example) Alice replies &amp;quot;Higher&amp;quot;. Then Bob says &amp;quot;2&amp;quot; and again Alice replies &amp;quot;Higher&amp;quot;, and so on. Eventually Bob says &amp;quot;40&amp;quot; and Alice says &amp;quot;Correct&amp;quot;, and the game is over. While this strategy is simple and correct, it may require up to 100 guesses to finish the game.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;However, there is &lt;/ins&gt;a &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;much cleverer strategy. Initially, Bob knows the number lies somewhere between 1 and 100. He guesses a number close to the ''middle'' of this range; say, 50. Alice replies, &amp;quot;Lower&amp;quot;. Now Bob knows the number lies in the range 1 to 49, so again he picks a number close to the middle of the range, say, 25. Alice replies, &amp;quot;Higher&amp;quot;, and the range has now been narrowed to 26 to 49. In this way, in every step, Bob will narrow the range to half its previous size. Eventually, either Bob will guess the number correctly, or there will only be one number left in the range, which must be Alice's number. Furthermore, this strategy will take at most 7 steps, because 100 halved 7 times is less than one. In general, the number of steps it takes Bob to find an integer between 1 and &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is at most about &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\log_2 N&lt;/ins&gt;&amp;lt;/math&amp;gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;whereas the naive &amp;quot;linear search&amp;quot; strategy introduced earlier may require up to &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;steps&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;We see that &lt;/ins&gt;the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;''bisection strategy'', or ''binary search'', is a vast improvement over the linear search.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Furthermore, binary search is the ''best possible strategy'' at Bob's disposal. This is because Bob asks a series &lt;/ins&gt;of &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;questions and gets a reply of &amp;quot;Higher&amp;quot; or &amp;quot;Lower&amp;quot; to each one except &lt;/ins&gt;the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;last (which is always &amp;quot;Correct&amp;quot;), and based on this information alone must determine the number; so the total number of possible answers that Bob can determine is at most the number of such sequences, which is approximately &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;2^N&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;if the length &lt;/ins&gt;is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;limited to &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;questions; so no strategy that asks less than &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\log_2 N&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;questions can guarantee being able to guess all possible &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; answers.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;==Finding an &lt;/ins&gt;element &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;in an array==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In &lt;/ins&gt;the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;most commonly encountered variation of binary search, we are given a zero-indexed [[array]] &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;A&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;of size &lt;/ins&gt;&amp;lt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;math&lt;/ins&gt;&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/ins&gt;&amp;lt;/&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;math&amp;gt; that is sorted in ascending order, along with some value &amp;lt;math&amp;gt;c&amp;lt;/math&lt;/ins&gt;&amp;gt;. There are two slightly different variations on what we wish to do:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Lower bound'': Here are two equivalent formulations:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* ''Lower bound'': Here are two equivalent formulations:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:# Find the ''first'' index into &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; where we could insert a new element of value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; without violating the ascending order.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:# Find the ''first'' index into &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; where we could insert a new element of value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; without violating the ascending order.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L15&quot; &gt;Line 15:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 24:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The element immediately preceding the lower bound is the last element with value less than &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The element immediately preceding the lower bound is the last element with value less than &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The element immediately preceding the upper bound is the last element with value less than or equal to &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The element immediately preceding the upper bound is the last element with value less than or equal to &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Of course, sometimes you only care whether the ''exact'' value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; you are seeking can be found in the array at all; but this is just a special case of lower bound.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Both lower bound and upper bound can easily be found by iterating through the array from beginning to end, which takes &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; comparisons and hence &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; time if the elements of the array are simple scalar variables that take &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time to compare. However, because the array is sorted, we can do better than this. We begin with the knowledge that the element we seek might be anywhere in the array, or it could be &amp;quot;just past the end&amp;quot;. Now, examine an element near the centre of the array. If this is less than the element we seek, we know the element we seek must be in the second half of the array. If it is greater, we know the element we seek must be in the first half. Thus, by making one comparison, we cut the range under consideration into half. It follows that after &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; comparisons, we will be able to locate either the upper bound or the lower bound as we desire.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Both lower bound and upper bound can easily be found by iterating through the array from beginning to end, which takes &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; comparisons and hence &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; time if the elements of the array are simple scalar variables that take &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time to compare. However, because the array is sorted, we can do better than this. We begin with the knowledge that the element we seek might be anywhere in the array, or it could be &amp;quot;just past the end&amp;quot;. Now, examine an element near the centre of the array. If this is less than the element we seek, we know the element we seek must be in the second half of the array. If it is greater, we know the element we seek must be in the first half. Thus, by making one comparison, we cut the range under consideration into half. It follows that after &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; comparisons, we will be able to locate either the upper bound or the lower bound as we desire.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L78&quot; &gt;Line 78:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 89:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Additionally, if the array can be very large, then &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; should be computed as &amp;lt;code&amp;gt;l + (r-l)/2&amp;lt;/code&amp;gt;, instead of &amp;lt;code&amp;gt;(l+r)/2&amp;lt;/code&amp;gt;, in order to avoid possible arithmetic overflow.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Additionally, if the array can be very large, then &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; should be computed as &amp;lt;code&amp;gt;l + (r-l)/2&amp;lt;/code&amp;gt;, instead of &amp;lt;code&amp;gt;(l+r)/2&amp;lt;/code&amp;gt;, in order to avoid possible arithmetic overflow.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Discrete: &lt;/del&gt;function==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Inverse &lt;/ins&gt;function &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;computation&lt;/ins&gt;==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;More generally, we have some monotone function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; which is easy to compute, &lt;/del&gt;and &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;we need to compute its inverse, which, on &lt;/del&gt;the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;face, seems to &lt;/del&gt;be &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;tricky. It is also not immediately obvious that we can make this abstraction from a given &lt;/del&gt;problem, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;but once &lt;/del&gt;the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;abstraction is discovered, a simple and efficient solution to the problem immediately follows&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Both the number guessing game &lt;/ins&gt;and the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;array search can &lt;/ins&gt;be &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;expressed as specific cases of the more general &lt;/ins&gt;problem &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;of ''discrete inverse function computation''&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;hinted at in &lt;/ins&gt;the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;lead section of this article&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;example &lt;/del&gt;problem we will &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;consider &lt;/del&gt;is... &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;TODO&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;We suppose:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# We are given some (straightforward to compute) function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; from a discrete [[totally ordered]] domain (such as the integers from the interval&amp;lt;math&amp;gt;[0, n)&amp;lt;/math&amp;gt;) to any totally ordered codomain (such as the real numbers);&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; is a monotonically increasing function, so that &amp;lt;math&amp;gt;a &amp;gt; b&amp;lt;/math&amp;gt; implies &amp;lt;math&amp;gt;f(a) \geq f(b)&amp;lt;/math&amp;gt;;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# We are given some value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt;'s codomain. (On the other hand, &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; may or may not be in &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt;'s ''range''.)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Here,&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;* &lt;/ins&gt;The &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;''lower bound'' is the smallest argument &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;f(x) \geq c&amp;lt;/math&amp;gt;, if any exists.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;* The ''upper bound'' is the smallest argument &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;f(x) &amp;gt; c&amp;lt;/math&amp;gt;, if any exists.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Sometimes there is exactly one &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;f(x) = c&amp;lt;/math&amp;gt;. This value can properly be denoted &amp;lt;math&amp;gt;x = f^{-1}(c)&amp;lt;/math&amp;gt;; finding the value &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is ''computing the inverse'' of &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; at &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;. Other times, there will be multiple such values, or there will only be &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;f(x) \approx c&amp;lt;/math&amp;gt;; in any case, binary search will find these values for us.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The approach mirrors that of the previous two sections. Initially we only know that &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; lies somewhere in the domain of &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt;, so we guess a value that roughly splits this range in half; by evaluating &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; at this point and comparing the result with &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, we can discard half the range and repeat as many times as necessary (about &amp;lt;math&amp;gt;O(\log |D|)&amp;lt;/math&amp;gt; times, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the domain).&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The number guessing game is a special case of this version of the &lt;/ins&gt;problem&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. One possible way to formulate it is that &amp;lt;math&amp;gt;f:[0, n) \to \mathbb{Z}&amp;lt;/math&amp;gt; is defined as &amp;lt;math&amp;gt;f(x) = 0&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; (the secret number), &amp;lt;math&amp;gt;f(x) = -1&amp;lt;/math&amp;gt; for all smaller numbers, and &amp;lt;math&amp;gt;f(x) = 1&amp;lt;/math&amp;gt; for all larger numbers; finding the secret number is just a matter of doing a binary search on &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The array search problem is also a special case. Here &lt;/ins&gt;we &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;simply define &amp;lt;math&amp;gt;f(x) = A_x&amp;lt;/math&amp;gt;. In fact, the two problems are in some sense equivalent, in that a function with a discrete totally ordered domain is isomorphic to an array; the distinction &lt;/ins&gt;will &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;be lost in any programming language that implements both lazy evaluation and [[memoization]].&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;More generally, binary search &lt;/ins&gt;is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;often very useful for problems that read something like &amp;quot;find the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;P(x)&amp;lt;/math&amp;gt;&amp;quot;&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;For example, in {{Problem|ccc03s5|Trucking Troubles}}, the problem essentially down to finding the maximum &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; such that the edges of the given graph of weight at least &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; span the graph&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;This problem can be solved using [[Prim's algorithm]] to construct the &amp;quot;maximum spanning tree&amp;quot; (really, the minimum spanning tree using negated edge weights), but binary search provides a simpler approach&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;We know that &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is somewhere in between the smallest edge weight in the graph and the largest edge weight in the graph, so we start by guessing a value roughly halfway in-between. Then we use [[depth-first search]] to test whether the graph is connected using only the edges of weight greater than or equal to our guess value. If it is, then the true value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is greater than or equal to our guess; if not, then it is less. This is then repeated as many times as necessary to determine the correct value of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Continuous==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Continuous==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;TODO&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;TODO&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==Problems==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;For some of these problems, binary search might not yield an optimal solution, or even get all the points; but the solution it gives far outperforms the [[naive algorithm]].&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|ccc10s3|Firehose}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|guess|Guess the Number}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|ccc08s2p6|Landing}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|ccc96s5|Max Distance}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{Problem|ccc03s5|Trucking Troubles}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|AGGRCOW|Aggressive Cows @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|BRI|Bridge @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|BRII|Bridges! More Bridges! @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|BOOKS1|Copying Books @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|GLASNICI|Glasnici @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|PIE|Pie @ SPOJ}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Incomplete]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Incomplete]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1556&amp;oldid=prev</id>
		<title>Brian at 08:47, 1 January 2012</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1556&amp;oldid=prev"/>
				<updated>2012-01-01T08:47:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 08:47, 1 January 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L67&quot; &gt;Line 67:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 67:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Possible bugs===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Possible bugs===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;One obvious bug is absentmindedly setting &lt;/del&gt;&amp;lt;code&amp;gt;r = n-1&amp;lt;/code&amp;gt; initially. Doing this will prevent the value &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; from ever being returned, which renders the implementation correct whenever the answer is less than &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, but incorrect whenever it is &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;. (It will simply return &amp;lt;code&amp;gt;n-1&amp;lt;/code&amp;gt; in this case.)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;As suggested below, the programmer has at least six opportunities to err.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Setting &lt;/ins&gt;&amp;lt;code&amp;gt;r = n-1&amp;lt;/code&amp;gt; initially. Doing this will prevent the value &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; from ever being returned, which renders the implementation correct whenever the answer is less than &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, but incorrect whenever it is &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;. (It will simply return &amp;lt;code&amp;gt;n-1&amp;lt;/code&amp;gt; in this case.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Using &amp;lt;code&amp;gt;while (l &amp;amp;lt;= r)&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;while (l &amp;amp;lt; r)&amp;lt;/code&amp;gt;. This will always cause an infinite loop in which the &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; branch is taken in every subsequent iteration.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Comparing &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; in the wrong direction, that is, writing &amp;lt;code&amp;gt;A[m] &amp;amp;gt; c&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;c &amp;amp;lt; A[m]&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;c &amp;amp;lt;= A[m]&amp;lt;/code&amp;gt;, or reversing the order of the two branches. This would not correspond to the theory, and would almost always given an incorrect result. Note that simultaneously logically negating the comparison and reversing the order of the branches, however, will not give an error, but will instead give logically equivalent code.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Computing &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; as the ceiling of the arithmetic mean, instead of the floor. The obvious error condition here is that when &amp;lt;code&amp;gt;l == n-1&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r == n&amp;lt;/code&amp;gt;, the value of &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; computed will be &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, and we will then attempt to examine a nonexistent element in the array, which could cause the program to crash.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Setting &amp;lt;code&amp;gt;l = m&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;l = m-1&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt;. Either variation lends itself to an error condition in which &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; comes out to be equal to either &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;l+1&amp;lt;/code&amp;gt;, respectively, and the first branch is taken, which causes an infinite loop in which the first branch is ''always'' taken.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;# Setting &amp;lt;code&amp;gt;r = m+1&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;r = m-1&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;. In the former case, we could have an infinite loop in which the second branch is always taken, if the difference between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; is 1 or 2. In the latter case, it is possible for &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; to become less than &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;And, of course, one can easily mix up the &amp;lt;code&amp;gt;upper_bound&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;lower_bound&amp;lt;/code&amp;gt; implementations, giving a seventh opportunity to err.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Another obvious bug is using &lt;/del&gt;&amp;lt;code&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;while (l &amp;amp;lt;= r)&lt;/del&gt;&amp;lt;/code&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;instead of &lt;/del&gt;&amp;lt;code&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;while (&lt;/del&gt;l &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;amp;lt; &lt;/del&gt;r)&amp;lt;/code&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;. This will always cause an infinite loop in which the &lt;/del&gt;&amp;lt;code&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;else&lt;/del&gt;&amp;lt;/code&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;branch is taken &lt;/del&gt;in &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;every subsequent iteration&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Additionally, if the array can be very large, then &lt;/ins&gt;&amp;lt;code&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;m&lt;/ins&gt;&amp;lt;/code&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;should be computed as &lt;/ins&gt;&amp;lt;code&amp;gt;l &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;+ (&lt;/ins&gt;r&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;-l&lt;/ins&gt;)&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;/2&lt;/ins&gt;&amp;lt;/code&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, instead of &lt;/ins&gt;&amp;lt;code&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;(l+r)/2&lt;/ins&gt;&amp;lt;/code&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, &lt;/ins&gt;in &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;order to avoid possible arithmetic overflow&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;A third obvious bug is either comparing &lt;/del&gt;&amp;lt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;code&lt;/del&gt;&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;A[m]&lt;/del&gt;&amp;lt;/&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;code&lt;/del&gt;&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; in the wrong direction, that &lt;/del&gt;is, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;writing &amp;lt;code&amp;gt;A[m] &amp;amp;gt; c&amp;lt;/code&amp;gt;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;code&amp;gt;c &amp;amp;lt; A[m]&amp;lt;/code&amp;gt;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;or &amp;lt;code&amp;gt;c &amp;amp;lt;= A[m]&amp;lt;/code&amp;gt;, or reversing the order of the two branches&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;This would &lt;/del&gt;not &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;correspond to &lt;/del&gt;the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;theory&lt;/del&gt;, and &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;would almost always given an incorrect result&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;==Discrete: function==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;More generally, we have some monotone function &lt;/ins&gt;&amp;lt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;math&lt;/ins&gt;&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;f&lt;/ins&gt;&amp;lt;/&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;math&lt;/ins&gt;&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;which &lt;/ins&gt;is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;easy to compute&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;and we need to compute its inverse&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;which&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;on the face&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;seems to be tricky&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;It is also &lt;/ins&gt;not &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;immediately obvious that we can make this abstraction from a given problem, but once &lt;/ins&gt;the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;abstraction is discovered&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;a simple &lt;/ins&gt;and &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;efficient solution to the problem immediately follows&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;However, there are still four degrees of freedom in the implementation:&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The example problem &lt;/ins&gt;we &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;will consider &lt;/ins&gt;is&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;... TODO.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;# Should &lt;/del&gt;we &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;compute &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; as the floor of the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, or as the ceiling?&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;# Should we compare &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; strictly, that &lt;/del&gt;is&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, with &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;, or non-strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;?&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Continuous&lt;/ins&gt;==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;# Should we set &amp;lt;code&amp;gt;l &lt;/del&gt;= &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;l &lt;/del&gt;= &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;l &lt;/del&gt;= &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;m-1&amp;lt;/code&amp;gt; in the first branch?&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;TODO&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;# Should we set &amp;lt;code&amp;gt;r = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;r &lt;/del&gt;= &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;m-1&amp;lt;/code&amp;gt; in the second branch?&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;These variations are covered in the page &lt;/del&gt;[[&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Binary search/Implementation details&lt;/del&gt;]]&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Category:Incomplete&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1555&amp;oldid=prev</id>
		<title>Brian: /* Possible bugs */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1555&amp;oldid=prev"/>
				<updated>2012-01-01T08:14:27Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Possible bugs&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 08:14, 1 January 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L74&quot; &gt;Line 74:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 74:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;However, there are still four degrees of freedom in the implementation:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;However, there are still four degrees of freedom in the implementation:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Should we compare &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;, or non-strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;?&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we compute &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; as the floor of the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, or as the ceiling?&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we compute &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; as the floor of the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, or as the ceiling?&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Should we compare &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;, or non-strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;?&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;l = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;l = m-1&amp;lt;/code&amp;gt; in the first branch?&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;l = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;l = m-1&amp;lt;/code&amp;gt; in the first branch?&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we set &amp;lt;code&amp;gt;r = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;r = m-1&amp;lt;/code&amp;gt; in the second branch?&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Should we set &amp;lt;code&amp;gt;r = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;r = m-1&amp;lt;/code&amp;gt; in the second branch?&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;These variations are covered in the page [[Binary search/Implementation details]].&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;These variations are covered in the page [[Binary search/Implementation details]].&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1554&amp;oldid=prev</id>
		<title>Brian: Created page with &quot;'''Binary search''' is the term used in computer science for the discrete version of the ''bisection method'', in which given a monotone function &lt;math&gt;f&lt;/math&gt; over a discrete i...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Binary_search&amp;diff=1554&amp;oldid=prev"/>
				<updated>2012-01-01T08:04:54Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Binary search&amp;#039;&amp;#039;&amp;#039; is the term used in computer science for the discrete version of the &amp;#039;&amp;#039;bisection method&amp;#039;&amp;#039;, in which given a monotone function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; over a discrete i...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;'''Binary search''' is the term used in computer science for the discrete version of the ''bisection method'', in which given a monotone function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; over a discrete interval &amp;lt;math&amp;gt;I&amp;lt;/math&amp;gt; and a target value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; one wishes to find &amp;lt;math&amp;gt;x \in I&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;f(x) \approx c&amp;lt;/math&amp;gt;. It is most often used to locate a value in a sorted [[array]]. The term may also be used for the continuous version.&lt;br /&gt;
&lt;br /&gt;
==Discrete: array==&lt;br /&gt;
In the most commonly encountered variation of binary search, we are given a zero-indexed [[array]] &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; of size &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; that is sorted in ascending order, along with some value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;. Here the domain of the function &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; is the set of indices into the array, and &amp;lt;math&amp;gt;f(i) = A_i&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the element at the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; position. There are two slightly different variations on what we wish to do:&lt;br /&gt;
* ''Lower bound'': Here are two equivalent formulations:&lt;br /&gt;
:# Find the ''first'' index into &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; where we could insert a new element of value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; without violating the ascending order.&lt;br /&gt;
:# If there is an element in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; with value greater than ''or equal to'' &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, return the index of the first such element; otherwise, return &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. In other words, return &amp;lt;math&amp;gt;\min\{i\,|\,A_i \geq c\}&amp;lt;/math&amp;gt;, or &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; if that set is empty.&lt;br /&gt;
* ''Upper bound'':&lt;br /&gt;
:# Find the ''last'' index into &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; where we could insert a new element of value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; without violating the ascending order.&lt;br /&gt;
:# If there is an element in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; with value ''strictly'' greater than to &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, return the index of the first such element; otherwise, return &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. In other words, return &amp;lt;math&amp;gt;\min\{i\,|\,A_i &amp;gt; c\}&amp;lt;/math&amp;gt;, or &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; if that set is empty.&lt;br /&gt;
The following properties then hold:&lt;br /&gt;
* If there is no &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_i = c&amp;lt;/math&amp;gt;, then the lower bound and upper bound will be the same; they will both point to the first element in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; that is greater than &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;.&lt;br /&gt;
:* If &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt; is larger than all elements in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, then the lower and upper bounds will both be &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&lt;br /&gt;
* If there is at least one element &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; with value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, then the lower bound will be the index of the ''first'' such element, and the upper bound will be the index of the element ''just after the last'' such element. Hence [lower bound, upper bound) is a [[half-open interval]] that represents the range of indices into &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; that refer to elements with value &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;.&lt;br /&gt;
* The element immediately preceding the lower bound is the last element with value less than &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;br /&gt;
* The element immediately preceding the upper bound is the last element with value less than or equal to &amp;lt;math&amp;gt;c&amp;lt;/math&amp;gt;, if this exists, or the hypothetical element at index -1, otherwise.&lt;br /&gt;
&lt;br /&gt;
Both lower bound and upper bound can easily be found by iterating through the array from beginning to end, which takes &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; comparisons and hence &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; time if the elements of the array are simple scalar variables that take &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time to compare. However, because the array is sorted, we can do better than this. We begin with the knowledge that the element we seek might be anywhere in the array, or it could be &amp;quot;just past the end&amp;quot;. Now, examine an element near the centre of the array. If this is less than the element we seek, we know the element we seek must be in the second half of the array. If it is greater, we know the element we seek must be in the first half. Thus, by making one comparison, we cut the range under consideration into half. It follows that after &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; comparisons, we will be able to locate either the upper bound or the lower bound as we desire.&lt;br /&gt;
&lt;br /&gt;
===Implementation===&lt;br /&gt;
However, implementation tends to be tricky, and even experienced programmers can introduce bugs when writing binary search. Correct reference implementations are given below in C:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* n is the size of the int[] array A. */&lt;br /&gt;
int lower_bound(int A[], int n, int c)&lt;br /&gt;
{&lt;br /&gt;
    int l = 0;&lt;br /&gt;
    int r = n;&lt;br /&gt;
    while (l &amp;lt; r)&lt;br /&gt;
    {&lt;br /&gt;
        int m = (l+r)/2;     /* Rounds toward zero. */&lt;br /&gt;
        if (A[m] &amp;lt; c)&lt;br /&gt;
            l = m+1;&lt;br /&gt;
        else&lt;br /&gt;
            r = m;&lt;br /&gt;
    }&lt;br /&gt;
    return l;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int upper_bound(int A[], int n, int c)&lt;br /&gt;
{&lt;br /&gt;
    int l = 0;&lt;br /&gt;
    int r = n;&lt;br /&gt;
    while (l &amp;lt; r)&lt;br /&gt;
    {&lt;br /&gt;
        int m = (l+r)/2;&lt;br /&gt;
        if (A[m] &amp;lt;= c)       /* Note use of &amp;lt; instead of &amp;lt;=. */&lt;br /&gt;
            l = m+1;&lt;br /&gt;
        else&lt;br /&gt;
            r = m;&lt;br /&gt;
    }&lt;br /&gt;
    return l;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Proof of correctness===&lt;br /&gt;
We prove that &amp;lt;code&amp;gt;lower_bound&amp;lt;/code&amp;gt; above is correct. The proof for &amp;lt;code&amp;gt;upper_bound&amp;lt;/code&amp;gt; is very similar.&lt;br /&gt;
&lt;br /&gt;
We claim the invariant that, immediately before and after each iteration of the &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop:&lt;br /&gt;
# The index we are seeking is at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;.&lt;br /&gt;
# The index we are seeking is at most &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;.&lt;br /&gt;
This is certainly true immediately before the first iteration of the loop, where &amp;lt;code&amp;gt;l = 0&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r = n&amp;lt;/code&amp;gt;. Now we claim that if it is true before a given iteration of the loop, it will be true after that iteration, which, by induction, implies our claim. First, the value &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; will be exactly the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, if their sum is even, or it will be 0.5 less than the mean, if their sum is odd, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is computed as the arithmetic mean rounded down. This implies that, in any given iteration of the loop, &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;'s initial value will be at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, and strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;. Now:&lt;br /&gt;
* ''Case 1'': &amp;lt;code&amp;gt;A[m] &amp;amp;lt; c&amp;lt;/code&amp;gt;. This means that no element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, can be greater than &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;, because the array is sorted. But because we know that such an element exists (or that it is the hypothetical element just past the end of the array) in the current range delineated by indices &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, we conclude that it must be in the second half of the range with indices from &amp;lt;code&amp;gt;m+1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive. In this case we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt; and at the end of the loop the invariant is again satisfied. This is guaranteed to increase the value of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out with value at least &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;. It also does not increase &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; beyond &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; is initially strictly less than &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;.&lt;br /&gt;
* ''Case 2'': &amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;. This means that there exists an element with index between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, inclusive, which is at least &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;. This implies that the first such element is somewhere in this half of the range, because the array is sorted. Therefore, after setting &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, the range of indices between &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, inclusive, will still contain the index we are seeking, and hence the invariant is again satisfied. This is guaranteed to decrease the value of &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out strictly less than &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;; it also will not decrease it beyond &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; starts out at least as large as &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;.&lt;br /&gt;
Since each iteration of the loop either increases &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; or decreases &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, the loop will eventually terminate with &amp;lt;code&amp;gt;l = r&amp;lt;/code&amp;gt;. Since the invariant still holds, this unique common value must be the solution. &amp;lt;math&amp;gt;_{\blacksquare}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Carefully proving the &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; time bound and showing the correctness of &amp;lt;code&amp;gt;upper_bound&amp;lt;/code&amp;gt; are left as exercises to the reader.&lt;br /&gt;
&lt;br /&gt;
===Possible bugs===&lt;br /&gt;
One obvious bug is absentmindedly setting &amp;lt;code&amp;gt;r = n-1&amp;lt;/code&amp;gt; initially. Doing this will prevent the value &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; from ever being returned, which renders the implementation correct whenever the answer is less than &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, but incorrect whenever it is &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;. (It will simply return &amp;lt;code&amp;gt;n-1&amp;lt;/code&amp;gt; in this case.)&lt;br /&gt;
&lt;br /&gt;
Another obvious bug is using &amp;lt;code&amp;gt;while (l &amp;amp;lt;= r)&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;while (l &amp;amp;lt; r)&amp;lt;/code&amp;gt;. This will always cause an infinite loop in which the &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; branch is taken in every subsequent iteration.&lt;br /&gt;
&lt;br /&gt;
A third obvious bug is either comparing &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; in the wrong direction, that is, writing &amp;lt;code&amp;gt;A[m] &amp;amp;gt; c&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;A[m] &amp;amp;gt;= c&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;c &amp;amp;lt; A[m]&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;c &amp;amp;lt;= A[m]&amp;lt;/code&amp;gt;, or reversing the order of the two branches. This would not correspond to the theory, and would almost always given an incorrect result.&lt;br /&gt;
&lt;br /&gt;
However, there are still four degrees of freedom in the implementation:&lt;br /&gt;
# Should we compare &amp;lt;code&amp;gt;A[m]&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;, or non-strictly, that is, with &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;?&lt;br /&gt;
# Should we compute &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; as the floor of the arithmetic mean of &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, or as the ceiling?&lt;br /&gt;
# Should we set &amp;lt;code&amp;gt;l = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;l = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;l = m-1&amp;lt;/code&amp;gt; in the first branch?&lt;br /&gt;
# Should we set &amp;lt;code&amp;gt;r = m+1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;r = m&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;r = m-1&amp;lt;/code&amp;gt; in the second branch?&lt;br /&gt;
These variations are covered in the page [[Binary search/Implementation details]].&lt;/div&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>