<?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>Talk Binary &#187; sorted</title>
	<atom:link href="http://talkbinary.com/tag/sorted/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +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>C++ Recursion &#8211; Binary Search</title>
		<link>http://talkbinary.com/programming/c/c-recursion-binary-search/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-binary-search/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 03:42:14 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[recursive function]]></category>
		<category><![CDATA[searching recursively]]></category>
		<category><![CDATA[sorted]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2433</guid>
		<description><![CDATA[Binary Search using recursion Binary search is a method to search through a sorted container in order to find a particular value. Remember that &#8220;Guess the number&#8221; game? Let&#8217;s look at the following scenario by applying the Binary Search Algorithm in order for you to understand how Binary Search works. The Guessing Game Pick a<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-binary-search/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h2 id="section-1">Binary Search using recursion</h2>
<p>Binary search is a method to search through a <strong>sorted</strong> container in order to find a particular value. </p>
<p>Remember that &#8220;Guess the number&#8221; game? Let&#8217;s look at the following scenario by applying the Binary Search Algorithm in order for you to understand how Binary Search works.</p>
<blockquote><h3 id="section-2">The Guessing Game</h3>
<p>Pick a number from 0 &#8211; 100 and depending on your guess, I&#8217;ll either say Correct, Higher, or Lower. What would you choose?</p>
<table>
<tr>
<td>0</td>
<td>1</td>
<td>&#8230;</td>
<td>49</td>
<td>50</td>
<td>51</td>
<td>&#8230;</td>
<td>99</td>
<td>100</td>
</tr>
</table>
<p>Well, 50 of course! Why? Best case you get it correct. Worst case you&#8217;ll either get a &#8220;Higher&#8221; or &#8220;Lower&#8221;. Now think about the following, if you got &#8220;Higher&#8221; you eliminated 0-49, or 51-100 if &#8220;Lower&#8221;, in other words, you eliminate HALF the possibilities.</p>
<table>
<tr>
<td>50</td>
<td>51</td>
<td>&#8230;</td>
<td>74</td>
<td>75</td>
<td>76</td>
<td>&#8230;</td>
<td>99</td>
<td>100</td>
</tr>
</table>
<p>Well, let&#8217;s say it was &#8220;Higher&#8221;. What would you guess after? 75. Since it&#8217;s in between 50 &#8211; 100. If you didn&#8217;t guess correctly, you&#8217;ll be facing the similar scenario as before. You&#8217;ll end up eliminating half the possibilities!
</p></blockquote>
<h2 id="section-3">Getting started on the Binary Search Algorithm</h2>
<p>Let&#8217;s break up the rules for the Binary Search Algorithm. </p>
<ul>
<li>If the number guessed is correct, return the location of the element</li>
<li>If the number is at a &#8220;Higher&#8221; location, eliminate the lower half, else eliminate the upper half</li>
</ul>
<p><br/>Given this information, what would we need for our function header?<br />
<span id="more-2433"></span></p>
<blockquote><h3 id="section-4">Header for Binary Search</h3>
<p>We&#8217;d need the container and values indicating where our lower and higher boundary are. We&#8217;ll be returning the location of the value, else -1 if not found. We&#8217;ll also need the key we are looking for. </p>
<pre lang="c++">int binarySearch(vector <int> vec, int low, int high, int key) {</pre>
</blockquote>
<p>Next, what would our base cases be? When the element we &#8220;pick&#8221; is correct, or we exhausted searching through our container. </p>
<blockquote><h3 id="section-5">Base Case for Binary Search</h3>
<p>Like mentioned before, the first element to be compared to in our container would be the middle element. We&#8217;d also check to see if we exhausted our search by the following base case.</p>
<pre lang="c++">int binarySearch(vector <int> vec, int low, int high, int key) {

    // We exhausted our search
    if ( low > high ) {
         return -1;
    } 

    // The middle element
    int mid = (low + high)/2;

    if ( vec[mid] == key ) {
        return mid;
    }
</pre>
<p><br/>If you don&#8217;t understand why we have our initial base case, follow along. You&#8217;ll understand shortly.
</p></blockquote>
<p>Next, how would we break down our Binary Search on the next recursive call? Well, by simply assigning a new low, or high depending if they key was higher or lower than the one compared to.</p>
<h2 id="section-6">Binary Search using Recursion</h2>
<pre lang="c++">int binarySearch(vector <int> vec, int low, int high, int key) {

    // We exhausted our search
    if ( low > high ) {
         return -1;
    } 

    // The middle element
    int mid = (low + high)/2;

    if ( vec[mid] == key ) {
        return mid;
    }

    //If middle index value is larger than our key,
    //we search the lower half of the vector
    //else we search the upper half

    else if ( vec[mid] > key ) {
         return binarySearch(vec, low, mid-1, key);
    } else {
         return binarySearch(vec, mid+1, high, key);
    }
}</pre>
<p><br/>If you didn&#8217;t understand our initial base case, run through this algorithm using our recursive calls on a small vector without the key you are looking for and you&#8217;ll realize why our base case works as follows. </p>
<h2 id="section-7">Going further</h2>
<ol>
<li>Can you make a <a href="http://talkbinary.com/programming/c/c-helper-functions/">helper function</a> to make this calling this function cleaner for the user? </li>
</ul>
<li>Would this algorithm work using an unsorted array?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-binary-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 13:22:37 -->
