<?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>Hello, I am Sean Murphy &#187; binary search</title>
	<atom:link href="http://iamseanmurphy.com/tag/binary-search/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamseanmurphy.com</link>
	<description>Thoughts, news, code by Sean Murphy</description>
	<lastBuildDate>Thu, 26 Jan 2012 02:37:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Binary Search for Javascript Arrays</title>
		<link>http://iamseanmurphy.com/2009/04/29/binary-search-for-javascript-arrays/</link>
		<comments>http://iamseanmurphy.com/2009/04/29/binary-search-for-javascript-arrays/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 21:41:12 +0000</pubDate>
		<dc:creator>Sean Murphy</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://iamseanmurphy.com/2009/04/29/binary-search-for-javascript-arrays/</guid>
		<description><![CDATA[If you need to search through a large array, or you search arrays frequently in your Javascript code, or if you do both, chances are a binary search will give you better performance than a linear search (read: for loop). One caveat, however, is that binary search algorithms only work on sorted arrays. Here is [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to search through a large array, or you search arrays frequently in your Javascript code, or if you do both, chances are a binary search will give you better performance than a linear search (read: for loop). One caveat, however, is that binary search algorithms only work on sorted arrays. Here is a binary search function I sometimes use in my code:</p>
<p><span id="more-34"></span></p>
<pre name="code" class="js">Array.prototype.binSearch = function(needle, case_insensitive) {
    if (!this.length) return -1;

	var high = this.length - 1;
	var low = 0;
	case_insensitive = (typeof(case_insensitive) !== 'undefined' &amp;&amp; case_insensitive) ? true:false;
	needle = (case_insensitive) ? needle.toLowerCase():needle;

	while (low &lt;= high) {
		mid = parseInt((low + high) / 2)
		element = (case_insensitive) ? this[mid].toLowerCase():this[mid];
		if (element &gt; needle) {
			high = mid - 1;
		} else if (element &lt; needle) {
			low = mid + 1;
		} else {
			return mid;
		}
	}

	return -1;
};</pre>
]]></content:encoded>
			<wfw:commentRss>http://iamseanmurphy.com/2009/04/29/binary-search-for-javascript-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

