<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>李保存 &#187; algorithm</title>
	<atom:link href="http://libaocun.com/tag/algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://libaocun.com</link>
	<description>Think globally, act locally.</description>
	<lastBuildDate>Fri, 02 Jul 2010 07:27:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Searching For a Number in Shifted Sorted Array within O(log(n)) time</title>
		<link>http://libaocun.com/searching-for-a-number-in-shifted-sorted-array-within-ologn-time</link>
		<comments>http://libaocun.com/searching-for-a-number-in-shifted-sorted-array-within-ologn-time#comments</comments>
		<pubDate>Wed, 29 Jul 2009 03:56:56 +0000</pubDate>
		<dc:creator>Bali</dc:creator>
				<category><![CDATA[English - 英文]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ACT]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[perf]]></category>

		<guid isPermaLink="false">http://www.balionweb.com/?p=58</guid>
		<description><![CDATA[Run into the algorithm problem long time ago. Now post my answer here. A sorted array, say: {1,2,3,4,5,6,7,8,9,10,11,12}, do right rotate through carry unknown times, and then it might become: {6,7,8,9,10,11,12,1,2,3,4,5}. Now we need get the index of a given number, say 4, from the array within O(log(n)) time. Apparently a 8-year-old can get it [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">Run into the algorithm problem long time ago. Now post my answer here. A sorted array, say: {1,2,3,4,5,6,7,8,9,10,11,12}, do right rotate through carry unknown times, and then it might become: {6,7,8,9,10,11,12,1,2,3,4,5}. Now we need get the index of a given number, say 4, from the array within O(log(n)) time. Apparently a 8-year-old can get it done with O(n) time.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">We can think of it this way: take the middle element of array, if target is found, fine; if not, and then array become two parts, one is sorted array, the other is shifted sorted array. As illustrated as below diagram:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small"><img src="http://lh3.ggpht.com/_A9xfBBUVmv4/SYhi1erxNKI/AAAAAAAABbg/GHue0bk-nLg/s800/ShiftedArray.gif" alt="" /></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">If the target falls into the sorted array half, we can simple do a binary search; otherwise, repeat this operation in the other half in recursive way. You can see this is divide-and-conquer algorithm. Obviously this is O(log(n)).</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><strong><span style="text-decoration: underline"><span lang="EN-US"><span style="font-size: small"><span style="font-family: Calibri">Code</span></span></span></span></strong></p>
<table class="MsoTableGrid" style="border: medium none;background: #eeece1 none repeat scroll 0% 0%;border-collapse: collapse" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border: medium none black;padding: 0cm 5.4pt;background-color: transparent;width: 644.6pt" width="859" valign="top">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">//</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">// A typical binary search implementation</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">//</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: blue;font-size: 10pt" lang="EN-US">int</span><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"> _BinarySearch(<span style="color: blue">unsigned</span> <span style="color: blue">int</span> ShiftedArray[], <span style="color: blue">unsigned</span> <span style="color: blue">int</span> start, <span style="color: blue">unsigned</span> <span style="color: blue">int</span> end, <span style="color: blue">unsigned</span> <span style="color: blue">int</span> target)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US">{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: green">// Not found</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>( start == end &amp;&amp; ShiftedArray[start] != target) {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> -1;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">unsigned</span> <span style="color: blue">int</span> middle = start + (end &#8211; start)/2;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>(target == ShiftedArray[middle])</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> middle;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> <span style="color: blue">if</span> (target &gt; ShiftedArray[middle]) {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> _BinarySearch(ShiftedArray, middle + 1, end, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> _BinarySearch(ShiftedArray, start, middle &#8211; 1, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US">}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">//</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">// Select a given number from shifted array.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">// ShiftedArray is something like = {6,7,8,9,10,11,12,1,2,3,4,5}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">// If found, return index of the number; if not, reutrn -1</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">// Require log(N)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: green;font-size: 10pt" lang="EN-US">//</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';color: blue;font-size: 10pt" lang="EN-US">int</span><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"> SearchShiftedArray(<span style="color: blue">unsigned</span> <span style="color: blue">int</span> ShiftedArray[], <span style="color: blue">unsigned</span> <span style="color: blue">int</span> start, <span style="color: blue">unsigned</span> <span style="color: blue">int</span> end, <span style="color: blue">unsigned</span> <span style="color: blue">int</span> target)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US">{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: green">// Start meets end</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>( start == end &amp;&amp; ShiftedArray[start] != target) {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> -1;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">unsigned</span> <span style="color: blue">int</span> middle = start + (end &#8211; start)/2;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>(target == ShiftedArray[middle])</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> middle;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> <span style="color: blue">if</span>(ShiftedArray[middle] &lt; ShiftedArray[start]) { <span style="color: green">// Right half is sorted linearly</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>((target &gt; ShiftedArray[middle]) &amp;&amp; (target &lt;= ShiftedArray[end])) {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> _BinarySearch(ShiftedArray, middle + 1, end, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> SearchShiftedArray(ShiftedArray, start, middle-1, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> { <span style="color: green">// Left half is sorted linearly</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">if</span>((target &gt;= ShiftedArray[start]) &amp;&amp; (target &lt; ShiftedArray[middle])) {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> _BinarySearch(ShiftedArray, start, middle &#8211; 1, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>} <span style="color: blue">else</span> {</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span><span style="color: blue">return</span> SearchShiftedArray(ShiftedArray, middle + 1, end, target);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span style="font-family: 'Courier New';font-size: 10pt" lang="EN-US">}</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><strong><span style="text-decoration: underline"><span lang="EN-US"> </span></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt"><strong><span style="text-decoration: underline"><span lang="EN-US"><span style="font-size: small"><span style="font-family: Calibri">Test cases</span></span></span></span></strong></p>
<table class="MsoTableGrid" style="border: medium none;background: #eeece1 none repeat scroll 0% 0%;border-collapse: collapse" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border: medium none black;padding: 0cm 5.4pt;background-color: transparent;width: 644.6pt" width="859" valign="top">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">Positive: {6,7,8,9,10,11,12,1,2,3,4,5}, target = 3, target = 8</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">Negative: {6,7,8,9,10,11,12,1,2,3,4,5}, target = 0, target = 13</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">Boundary: {6,7,8,9,10,11,12,1,2,3,4,5}, target = 6, target = 5</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt"><span lang="EN-US"><span style="font-family: Calibri;font-size: small">Exceptional: {…max}, target = max</span></span></p>
</td>
</tr>
</tbody>
</table>
<p><span style="font-family: 'Calibri','sans-serif';font-size: 11pt" lang="EN-US"> </span><span style="font-family: 'Calibri','sans-serif';font-size: 11pt" lang="EN-US">One more interesting thing is the statement that “<em><span style="color: red">only about 10 percent of the professional programmers implemented binary search correctly.</span></em>” Do you know why? Check <a href="http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html">this</a>.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://libaocun.com/searching-for-a-number-in-shifted-sorted-array-within-ologn-time/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting median of two sorted arrays</title>
		<link>http://libaocun.com/selecting-median-of-two-sorted-arrays</link>
		<comments>http://libaocun.com/selecting-median-of-two-sorted-arrays#comments</comments>
		<pubDate>Wed, 29 Jul 2009 03:18:48 +0000</pubDate>
		<dc:creator>Bali</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ACT]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[perf]]></category>

		<guid isPermaLink="false">http://www.balionweb.com/?p=26</guid>
		<description><![CDATA[In  this post, I’d like to discuss one interesting algorithm problem which took me  quite a while to find an ideal solution. The problem is as  followings:
Array  A and B are sorted with length of m and n respectively. Try to select median of  the two arrays within O(log(m+n)) time. [...]]]></description>
			<content:encoded><![CDATA[<p>In  this post, I’d like to discuss one interesting algorithm problem which took me  quite a while to find an ideal solution. The problem is as  followings:</p>
<p>Array  A and B are sorted with length of m and n respectively. Try to select median of  the two arrays within O(log(m+n)) time. For example,</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="590" valign="top">A = { 1, 4, 6, 10, 18 }</p>
<p>B = { 1, 7, 13, 45, 58, 69, 100, 180, 300 }</p>
<p>Answer: 13</td>
</tr>
</tbody>
</table>
<p><strong><span style="text-decoration: underline">Step1: The lengths are equal.</span></strong> Let us start with a special scenario of the problem. The solution is as  following.</p>
<p>Examine the middle element of each array, and throw out the lower  half of the array with the smaller element (since all those must be less than ½  the numbers) and throw out the upper half of the array with the larger element  (since all those must be greater than ½ the numbers).  Now both arrays are still  the same size.  Repeat until you have two elements left.  This is your median.   Each step, you eliminate half of the numbers, so it should have a runtime of  O(logn).</p>
<p><strong><span style="text-decoration: underline">Step2:  What if the lengths are not equal? </span></strong></p>
<p>The smartest solution I’ve found is padding. It is best to think of median  as a special case of the select problem, where given a set and an index k, you  need to find the kth smallest element. Median is simply Select(n/2) when n is  even, and fully defined by Select(\floor(n/2)) and Select (\Ceiling(n/2)) when n  is odd. Now there is a simple O(log n) algorithm for the Select problem on the  union of two equal sorted arrays, that throws out the appropriate halves of the  two lists recursively.</p>
<p>If  you want a simpler reduction to equal sized lists, pad the shorter list with an  equal number of positive and negative infinities. If you need an odd number of  pads (ie: when the source lists have an odd total), do it with positive infinity  and (since the set size was odd) return the lesser of the two retuned  values.</p>
]]></content:encoded>
			<wfw:commentRss>http://libaocun.com/selecting-median-of-two-sorted-arrays/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
