<?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; pseudocode</title>
	<atom:link href="http://talkbinary.com/tag/pseudocode/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Binary Search in C++</title>
		<link>http://talkbinary.com/uncategorized/binary-search-in-c/</link>
		<comments>http://talkbinary.com/uncategorized/binary-search-in-c/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 08:53:55 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[pseudo-code]]></category>
		<category><![CDATA[pseudocode]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1393</guid>
		<description><![CDATA[Binary Search is a method to search through a sorted container in order to find a particular value. Remember that one &#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 number from 0<a class="moretag" href="http://talkbinary.com/uncategorized/binary-search-in-c/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Binary Search is a method to search through a sorted container in order to find a particular value.</p>
<p>Remember that one &#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-1">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>
<p><span id="more-1393"></span></p>
<h3 id="section-2">Binary Search Algorithm</h3>
<p>The following is the recursive implementation. </p>
<pre lang="c++">int binarysearch(vector <int> vec, int low, int high, int key)
{
    //Not found
    if ( low > high )
    {
          return -1;
    }

    //Finding the middle index between high and low
    int mid = (low + high)/2;

    //Returning the index if key is found
    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 of the vector
    else if ( vec[mid] > key )
    {
          return binarysearch(vec, low, mid-1, key);
    }
    else
    {
          return binarysearch(vec, mid+1, high, key);
    }
}</pre>
<h3 id="section-3">Example of Binary Search</h3>
<p>Let&#8217;s say our container held the values 0-100 in its respective index values. Let&#8217;s say the value we are looking for is 57. </p>
<h4 id="section-4">First iteration</h4>
<table width="90%">
<tr>
<td>Index:</td>
<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>
<tr>
<td>Value</td>
<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>
<tr>
<td>Parameters:</td>
<td>low</td>
<td></td>
<td></td>
<td></td>
<td>mid</td>
<td></td>
<td></td>
<td></td>
<td>high</td>
</tr>
</table>
<p>50 < 57, so search through the upper half of our vector.</p>
<h4 id="section-5">Second iteration</h4>
<table width="90%">
<tr>
<td>Index:</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>74</td>
<td>75</td>
<td>76</td>
<td>&#8230;</td>
<td>99</td>
<td>100</td>
</tr>
<tr>
<td>Value</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>74</td>
<td>75</td>
<td>76</td>
<td>&#8230;</td>
<td>99</td>
<td>100</td>
</tr>
<tr>
<td>Parameters:</td>
<td>low</td>
<td></td>
<td></td>
<td></td>
<td>mid</td>
<td></td>
<td></td>
<td></td>
<td>high</td>
</tr>
</table>
<p>75 > 57, so search through the lower half of our vector.</p>
<h4 id="section-6">Third iteration</h4>
<table width="90%">
<tr>
<td>Index:</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>62</td>
<td>63</td>
<td>64</td>
<td>&#8230;</td>
<td>73</td>
<td>74</td>
</tr>
<tr>
<td>Value</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>62</td>
<td>63</td>
<td>64</td>
<td>&#8230;</td>
<td>73</td>
<td>74</td>
</tr>
<tr>
<td>Parameters:</td>
<td>low</td>
<td></td>
<td></td>
<td></td>
<td>mid</td>
<td></td>
<td></td>
<td></td>
<td>high</td>
</tr>
</table>
<p>63 > 57, so search through the lower half of our vector.</p>
<h4 id="section-7">Fourth iteration</h4>
<table width="90%">
<tr>
<td>Index:</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>56</td>
<td>57</td>
<td>58</td>
<td>&#8230;</td>
<td>61</td>
<td>62</td>
</tr>
<tr>
<td>Value</td>
<td>51</td>
<td>52</td>
<td>&#8230;</td>
<td>56</td>
<td>57</td>
<td>58</td>
<td>&#8230;</td>
<td>61</td>
<td>62</td>
</tr>
<tr>
<td>Parameters:</td>
<td>low</td>
<td></td>
<td></td>
<td></td>
<td>mid</td>
<td></td>
<td></td>
<td></td>
<td>high</td>
</tr>
</table>
<p>57 == 57!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/uncategorized/binary-search-in-c/feed/</wfw:commentRss>
		<slash:comments>3</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-05-24 09:21:11 -->
