<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.5" -->
<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/"
	>

<channel>
	<title>DataStructures</title>
	<link>http://www.datastructures.info</link>
	<description>Data structures, algorithms, e-books, usefull code and so on...</description>
	<pubDate>Thu, 01 Mar 2007 09:27:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>
	<language>en</language>
			<item>
		<title>What are Hash Tables and how do they work?</title>
		<link>http://www.datastructures.info/what-are-hash-tables-and-how-do-they-work/</link>
		<comments>http://www.datastructures.info/what-are-hash-tables-and-how-do-they-work/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 09:27:58 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-are-hash-tables-and-how-do-they-work/</guid>
		<description><![CDATA[
As we saw with binary search, certain data structures such as a binary search tree can help improve the efficiency of searches. From linear search to binary search, we improved our search efficiency from O(n) to O(logn). We now present a new data structure, called a hash table, that will increase our efficiency to O(1), [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="What are Hash Tables and how do they work?" id="image39" src="http://www.datastructures.info/wp-content/uploads/2007/03/normal.png" /></p>
<p>As we saw with binary search, certain data structures such as a binary search tree can help improve the efficiency of searches. From linear search to binary search, we improved our search efficiency from O(n) to O(logn). We now present a new data structure, called a hash table, that will increase our efficiency to O(1), or constant time.<a id="more-41"></a></p>
<p>In computer science, a hash table, or a hash map, is a data structure that associates keys with values. The primary operation it supports efficiently is a lookup: given a key (e.g. a person&#8217;s name), find the corresponding value (e.g. that person&#8217;s telephone number). It works by transforming the key using a hash function into a hash, a number that is used to index into an array to locate the desired location (&#8221;bucket&#8221;) where the values should be.</p>
<p><!--adsense--></p>
<p>Hash tables are often used to implement associative arrays, sets and caches. Like arrays, hash tables provide constant-time O(1) lookup on average, regardless of the number of items in the table. (O(1) means that it takes a constant amount of time independent of the number of items involved.) However, the rare worst-case lookup time can be as bad as O(n). Compared to other associative array data structures, hash tables are most useful when large numbers of records are to be stored, especially if the size of the data set can be predicted.</p>
<p><img alt="What are Hash Tables and how do they work?" id="image40" src="http://www.datastructures.info/wp-content/uploads/2007/03/hash.png" /></p>
<p>Hash tables may be used as in-memory data structures. Hash tables may also be adopted for use with persistent data structures; database indexes sometimes use disk-based data structures based on hash tables, although balanced trees are more popular.</p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">//this program creates a hash table as a set of linked lists</span><br />
<span class="co1">// here 10 pointers are used to address 10 linked lists which form a hash tablw</span><br />
<span class="co2">#include&lt;stdio.h&gt;</span><br />
<span class="co2">#include&lt;malloc.h&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> i,k,num;<br />
<span class="kw4">struct</span> node<br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> n;<br />
<span class="kw4">struct</span> node* next;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="kw4">struct</span> node *temp,*p1,*AOP<span class="br0">&#91;</span><span class="nu0">10</span><span class="br0">&#93;</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>;i&lt;<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
AOP<span class="br0">&#91;</span>i<span class="br0">&#93;</span>= <span class="kw2">NULL</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span> i=<span class="nu0">0</span>;i&lt;<span class="nu0">20</span>;i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
scanf<span class="br0">&#40;</span><span class="st0">&#8220;%d&#8221;</span>, &amp;num<span class="br0">&#41;</span>;<br />
k=num%<span class="nu0">10</span>;<br />
p1=<span class="br0">&#40;</span> <span class="kw4">struct</span> node*<span class="br0">&#41;</span> malloc <span class="br0">&#40;</span> <span class="kw4">sizeof</span><span class="br0">&#40;</span><span class="kw4">struct</span> node<span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>p1-&gt;n=num;<br />
p1-&gt;next=<span class="kw2">NULL</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>AOP<span class="br0">&#91;</span>k<span class="br0">&#93;</span>==<span class="kw2">NULL</span><span class="br0">&#41;</span><br />
AOP<span class="br0">&#91;</span>k<span class="br0">&#93;</span>=p1;<br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
p1-&gt;next=AOP<span class="br0">&#91;</span>k<span class="br0">&#93;</span>;<br />
AOP<span class="br0">&#91;</span>k<span class="br0">&#93;</span>=p1;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>;i&lt;<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
temp=AOP<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;List number   %d :&#8221;</span>, i<span class="br0">&#41;</span>;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>temp!=<span class="kw2">NULL</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%5d&#8221;</span>,temp-&gt;n<span class="br0">&#41;</span>;<br />
temp=temp-&gt;next;<br />
<span class="br0">&#125;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;<span class="es0">\n</span>&#8220;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link--->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-are-hash-tables-and-how-do-they-work/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is a Binary seach algorithm and how does it work?</title>
		<link>http://www.datastructures.info/what-is-a-binary-seach-algorithm-and-how-does-it-work/</link>
		<comments>http://www.datastructures.info/what-is-a-binary-seach-algorithm-and-how-does-it-work/#comments</comments>
		<pubDate>Thu, 25 Jan 2007 12:39:36 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-a-binary-seach-algorithm-and-how-does-it-work/</guid>
		<description><![CDATA[
A binary search algorithm (or binary chop) is a technique for finding a particular value in a linear array, by ruling out half of the data at each step, widely but not exclusively used in computer science. 
A binary search finds the median, makes a comparison to determine whether the desired value comes before or [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="What is a Binary seach algorithm and how does it work? " id="image37" src="http://www.datastructures.info/wp-content/uploads/2007/01/binary_search.jpg" /></p>
<p>A binary search algorithm (or binary chop) is a technique for finding a particular value in a linear array, by ruling out half of the data at each step, widely but not exclusively used in computer science. <a id="more-38"></a></p>
<p>A binary search finds the median, makes a comparison to determine whether the desired value comes before or after it, and then searches the remaining half in the same manner. Another explanation would be: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.<br />
Its running time is O(logn).</p>
<p><!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include&lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="co1">//Divide and conquer strategy - Binary Search</span><br />
<span class="coMULTI">/*This algorithm is the simplest application of divide and conquer.<br />
It finds a particular value in a linear array by ruling out half<br />
of the data at each step.A binary search finds the median, makes<br />
a comparision to determine whether the desired value comes before or<br />
after it, and then searches the remaining half in the same manner.<br />
However, the binary search algorithm applies only to sorted arrays.*/</span></p>
<p><span class="kw4">int</span> sorted_array<span class="br0">&#91;</span><span class="nu0">100</span><span class="br0">&#93;</span>;<br />
<span class="kw4">int</span> i=<span class="nu0">0</span>;</p>
<p><span class="kw4">int</span> binary_search<span class="br0">&#40;</span><span class="kw4">int</span> target<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> first = <span class="nu0">0</span>;<br />
<span class="kw4">int</span> last = i-<span class="nu0">1</span>;<br />
<span class="kw4">int</span> mid;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>first&lt;=last<span class="br0">&#41;</span> <span class="co1">//while we haven&#8217;t reached the end of</span><br />
<span class="br0">&#123;</span><br />
mid = <span class="br0">&#40;</span>first+last<span class="br0">&#41;</span>/<span class="nu0">2</span>; <span class="co1">//rule out half of the data by spliting the array</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>sorted_array<span class="br0">&#91;</span>mid<span class="br0">&#93;</span>==target<span class="br0">&#41;</span> <span class="co1">//if we have found the target</span><br />
<span class="kw1">return</span> mid;<br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>sorted_array<span class="br0">&#91;</span>mid<span class="br0">&#93;</span>&gt;target<span class="br0">&#41;</span><br />
last = mid-<span class="nu0">1</span>; <span class="co1">//the desired value is in the lower part of the array</span><br />
<span class="kw1">else</span><br />
first = mid+<span class="nu0">1</span>;<span class="co1">//the desired value is in the upper part of the array</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">return</span> -<span class="nu0">1</span>;   <span class="co1">//there is no such element in the array</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> j, n;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>cin&gt;&gt;j<span class="br0">&#41;</span><br />
sorted_array<span class="br0">&#91;</span>i++<span class="br0">&#93;</span>=j;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>cin&gt;&gt;n<span class="br0">&#41;</span> <span class="co1">//search for an element n in the array</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> result = binary_search<span class="br0">&#40;</span>n<span class="br0">&#41;</span>; <span class="co1">//position of the element n in the array</span><br />
cout&lt;&lt;result&lt;&lt;endl;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-a-binary-seach-algorithm-and-how-does-it-work/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Huffman encoding algorithm</title>
		<link>http://www.datastructures.info/huffman-encoding-algorithm/</link>
		<comments>http://www.datastructures.info/huffman-encoding-algorithm/#comments</comments>
		<pubDate>Wed, 24 Jan 2007 13:38:43 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/huffman-encoding-algorithm/</guid>
		<description><![CDATA[
Data transmission and storage cost money. The more information being dealt with, the more it costs. In spite of this, most digital data are not stored in the most compact form. Rather, they are stored in whatever way makes them easiest to use, such as: ASCII text from word processors, binary code that can be [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="Huffman encoding algorithm" id="image35" src="http://www.datastructures.info/wp-content/uploads/2007/01/huffman_encoding.jpg" /></p>
<p>Data transmission and storage cost money. The more information being dealt with, the more it costs. In spite of this, most digital data are not stored in the most compact form. Rather, they are stored in whatever way makes them easiest to use, such as: ASCII text from word processors, binary code that can be executed on a computer, individual samples from a data acquisition system, etc.<a id="more-36"></a></p>
<p>Data compression is the general term for the various algorithms and programs developed to address this problem. A compression program is used to convert data from an easy-to-use format to one optimized for compactness. Likewise, an uncompression program returns the information to its original form.</p>
<p>There are many different reasons for and ways of encoding data, and one of these ways is Huffman coding. This is used as a compression method in digital imaging and video as well as in other areas. The idea behind Huffman coding is simply to use shorter bit patterns for more common characters, and longer bit patterns for less common characters.</p>
<p>Huffman coding is based on the frequency of occurance of a data item (pixel in images). The principle is to use a lower number of bits to encode the data that occurs more frequently.</p>
<p><!--adsense--></p>
<p>The first version of Huffman encoding</p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
<span class="co2">#include &lt;map&gt;  // a multimap will be used so we have to include it</span></p>
<p>using namespace std;</p>
<p><span class="kw4">void</span> binary<span class="br0">&#40;</span><span class="kw4">int</span> number<span class="br0">&#41;</span> <span class="br0">&#123;</span>  <span class="co1">//thise procedure is used to convert a</span><br />
<span class="kw4">int</span> remainder;          <span class="co1">//number into a binary number</span></p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>number &lt;= <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; number;<br />
<span class="kw1">return</span>;<br />
<span class="br0">&#125;</span></p>
<p>remainder = number%<span class="nu0">2</span>;<br />
binary<span class="br0">&#40;</span>number &gt;&gt; <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; remainder;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">char</span> ch, alphabet<span class="br0">&#91;</span><span class="nu0">31</span><span class="br0">&#93;</span>;   <span class="co1">//store each char in ch, 31 chars used because of 26 alphabet, dot, comma, space</span><br />
<span class="kw4">typedef</span> multimap&lt;int,char&gt; huffman; <span class="co1">//make a multimap that will be sorted by integers, we use a multimap</span><br />
huffman sentence;                   <span class="co1">//because some characters may have the same number of occurences</span></p>
<p><span class="kw4">int</span> i=<span class="nu0">0</span>,j=<span class="nu0">0</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=<span class="nu0">0</span>; j&lt;<span class="nu0">31</span>; j++<span class="br0">&#41;</span>      <span class="co1">//alphabet[0..30] = 0;</span><br />
alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>=<span class="nu0">0</span>;</p>
<p><span class="kw1">while</span><span class="br0">&#40;</span><span class="br0">&#40;</span>ch=getchar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>!=<span class="st0">&#8216;<span class="es0">\n</span>&#8216;</span><span class="br0">&#41;</span> <span class="co1">//insert chars into ch until enter(return) is pressed</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>ch==<span class="st0">&#8216; &#8216;</span><span class="br0">&#41;</span><br />
alphabet<span class="br0">&#91;</span><span class="nu0">28</span><span class="br0">&#93;</span>++;     <span class="co1">//increment number of spaces</span><br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>ch==<span class="st0">&#8216;,&#8217;</span><span class="br0">&#41;</span><br />
alphabet<span class="br0">&#91;</span><span class="nu0">29</span><span class="br0">&#93;</span>++;     <span class="co1">//increment number of commas</span><br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>ch==<span class="st0">&#8216;.&#8217;</span><span class="br0">&#41;</span><br />
alphabet<span class="br0">&#91;</span><span class="nu0">30</span><span class="br0">&#93;</span>++;     <span class="co1">//increment number of dots</span><br />
<span class="kw1">else</span><span class="br0">&#123;</span><br />
ch = toupper<span class="br0">&#40;</span>ch<span class="br0">&#41;</span>;    <span class="co1">//convert all chars to upper case</span><br />
alphabet<span class="br0">&#91;</span><span class="br0">&#40;</span>static_cast&lt;int&gt;<span class="br0">&#40;</span>ch<span class="br0">&#41;</span><span class="br0">&#41;</span>-<span class="nu0">65</span><span class="br0">&#93;</span>++;<span class="co1">//increment (char ASCII value) - 65 (this is the index number)</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=<span class="nu0">0</span>; j&lt;<span class="nu0">31</span>; j++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>&gt;<span class="nu0">0</span><span class="br0">&#41;</span>   <span class="co1">//check if char occured in the text</span><br />
<span class="br0">&#123;</span><br />
i++;  <span class="co1">//count the number of found chars, commas, dots, spaces</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>j==<span class="nu0">28</span><span class="br0">&#41;</span><br />
sentence.<span class="me1">insert</span><span class="br0">&#40;</span>make_pair<span class="br0">&#40;</span>alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>,<span class="st0">&#8216; &#8216;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;  <span class="co1">//make the pair from space and its occurences</span><br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>j==<span class="nu0">29</span><span class="br0">&#41;</span><br />
sentence.<span class="me1">insert</span><span class="br0">&#40;</span>make_pair<span class="br0">&#40;</span>alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>,<span class="st0">&#8216;,&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;  <span class="co1">//make the part from comma and its occurences</span><br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>j==<span class="nu0">30</span><span class="br0">&#41;</span><br />
sentence.<span class="me1">insert</span><span class="br0">&#40;</span>make_pair<span class="br0">&#40;</span>alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>,<span class="st0">&#8216;.&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;  <span class="co1">//make the pair from dot and its occurences</span><br />
<span class="kw1">else</span><br />
sentence.<span class="me1">insert</span><span class="br0">&#40;</span>make_pair<span class="br0">&#40;</span>alphabet<span class="br0">&#91;</span>j<span class="br0">&#93;</span>,static_cast&lt;char&gt;<span class="br0">&#40;</span><span class="nu0">65</span>+j<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">//make the pair from chars and its occurences</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>huffman::<span class="me2">iterator</span> pos;   <span class="co1">//make an iterator for the multimap</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>pos = sentence.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; pos!=sentence.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; ++pos<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
j++;  <span class="co1">//number of different chars</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&#8220;Letter &#8220;</span> &lt;&lt; pos-&gt;second &lt;&lt; <span class="st0">&#8221; &#8220;</span>;  <span class="co1">//print out the letter and then its occurence code</span><br />
binary<span class="br0">&#40;</span>i-j<span class="br0">&#41;</span>;   <span class="co1">//convert the number to binary</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; endl;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link--></p>
<p>The second version of Huffman encoding by Una Benlic</p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include&lt;iostream&gt;</span><br />
<span class="co2">#include&lt;algorithm&gt;</span><br />
<span class="co2">#include&lt;string&gt;</span><br />
<span class="co2">#include&lt;map&gt;</span></p>
<p>using namespace std;</p>
<p><span class="kw4">char</span> ch<span class="br0">&#91;</span><span class="nu0">100</span><span class="br0">&#93;</span>; <span class="co1">//the characters in the text</span><br />
<span class="kw4">string</span> binary<span class="br0">&#91;</span><span class="nu0">100</span><span class="br0">&#93;</span>; <span class="co1">//binary representation of each character</span><br />
<span class="kw4">int</span> c=<span class="nu0">0</span>; <span class="co1">//number of distinct characters</span></p>
<p><span class="kw4">string</span> reverse<span class="br0">&#40;</span><span class="kw4">string</span> line<span class="br0">&#41;</span> <span class="co1">//procedure that reverses a line</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">string</span> result=<span class="st0">&#8220;&#8221;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=line.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-<span class="nu0">1</span>; i&gt;=<span class="nu0">0</span>; i&#8211;<span class="br0">&#41;</span><br />
result+=line<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
<span class="kw1">return</span> result;<br />
<span class="br0">&#125;</span><br />
<span class="kw4">string</span> to_binary<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span> <span class="co1">//procedure that converts a number to it&#8217;s binary representaion</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">string</span> result=<span class="st0">&#8220;&#8221;</span>;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>num/<span class="nu0">2</span>!=<span class="nu0">0</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
result+=<span class="br0">&#40;</span>num%<span class="nu0">2</span><span class="br0">&#41;</span>+<span class="st0">&#8216;0&#8242;</span>;<br />
num/=<span class="nu0">2</span>;<br />
<span class="br0">&#125;</span><br />
result+=<span class="br0">&#40;</span>num%<span class="nu0">2</span><span class="br0">&#41;</span>+<span class="st0">&#8216;0&#8242;</span>;<br />
<span class="kw1">return</span> reverse<span class="br0">&#40;</span>result<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">//Once a Huffman code has been generated, data may be encoded simply by</span><br />
<span class="co1">//replacing each symbol with it&#8217;s code</span><br />
<span class="kw4">string</span> *encode<span class="br0">&#40;</span><span class="kw4">string</span> text<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">string</span>* array= new <span class="kw4">string</span><span class="br0">&#91;</span>text.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>; <span class="co1">//array that stores a binary number associated</span><br />
<span class="co1">//with each character of the text</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;text.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i++<span class="br0">&#41;</span> <span class="co1">//for each character of the text</span><br />
<span class="br0">&#123;</span>                                <span class="co1">//find its binary representation</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=<span class="nu0">0</span>; j&lt;c; j++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>text<span class="br0">&#91;</span>i<span class="br0">&#93;</span>==ch<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="co1">//if that binary number matches the specified character in text</span><br />
array<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=binary<span class="br0">&#91;</span>j<span class="br0">&#93;</span>; <span class="co1">//put the binary number in the array</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">return</span> array;  <span class="co1">//return the array of encoded text</span><br />
<span class="br0">&#125;</span><br />
<span class="kw4">string</span> decode<span class="br0">&#40;</span><span class="kw4">string</span> array<span class="br0">&#91;</span><span class="br0">&#93;</span>, <span class="kw4">int</span> len<span class="br0">&#41;</span><span class="co1">//this procedure decodes the Huffman&#8217;s encoding</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">string</span> result=<span class="st0">&#8220;&#8221;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;len; i++<span class="br0">&#41;</span> <span class="co1">//for each binary number find a matching character</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=<span class="nu0">0</span>; j&lt;c; j++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>binary<span class="br0">&#91;</span>j<span class="br0">&#93;</span>==array<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
result+=ch<span class="br0">&#91;</span>j<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">return</span> result; <span class="co1">//return the decoded text</span><br />
<span class="br0">&#125;</span><br />
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">string</span> line=<span class="st0">&#8220;&#8221;</span>;<br />
<span class="kw4">string</span> text=<span class="st0">&#8220;&#8221;</span>;<br />
<span class="kw4">int</span> occurence<span class="br0">&#91;</span><span class="nu0">100</span><span class="br0">&#93;</span>,i,j;<br />
map&lt;char, int&gt;counter; <span class="co1">//holds thee character and the number of their occurence</span><br />
<span class="kw1">while</span><span class="br0">&#40;</span>getline<span class="br0">&#40;</span>cin, line<span class="br0">&#41;</span> and line.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>&gt;<span class="nu0">0</span><span class="br0">&#41;</span><br />
text+=line;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>; i&lt;text.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i++<span class="br0">&#41;</span><br />
counter<span class="br0">&#91;</span>text<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span>++; <span class="co1">//counts the number of occurences of each character</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>map&lt;char, int&gt;::<span class="me2">const_iterator</span> iter = counter.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; iter!=counter.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; iter++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
ch<span class="br0">&#91;</span>c<span class="br0">&#93;</span>=iter-&gt;first;<br />
occurence<span class="br0">&#91;</span>c<span class="br0">&#93;</span>=iter-&gt;second;<br />
c++;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>; i&lt;c; i++<span class="br0">&#41;</span> <span class="co1">//bubble sort of characters according to the number of their occurences</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>j=<span class="nu0">1</span>; j&lt;c; j++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>occurence<span class="br0">&#91;</span>j-<span class="nu0">1</span><span class="br0">&#93;</span>&lt;occurence<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
swap<span class="br0">&#40;</span>occurence<span class="br0">&#91;</span>j-<span class="nu0">1</span><span class="br0">&#93;</span>, occurence<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
swap<span class="br0">&#40;</span>ch<span class="br0">&#91;</span>j-<span class="nu0">1</span><span class="br0">&#93;</span>, ch<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>; i&lt;c; i++<span class="br0">&#41;</span> <span class="co1">//assign a binary value to each character</span><br />
binary<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=to_binary<span class="br0">&#40;</span>i<span class="br0">&#41;</span>;</p>
<p><span class="kw4">string</span> *encoded = encode<span class="br0">&#40;</span>text<span class="br0">&#41;</span>;<span class="co1">//encode the text into Huffman&#8217;s representation</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>; i&lt;text.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i++<span class="br0">&#41;</span><span class="co1">//print the encoded text</span><br />
cout&lt;&lt;encoded<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
cout&lt;&lt;endl;<br />
<span class="kw4">string</span> decoded = decode<span class="br0">&#40;</span>encoded, text.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<span class="co1">//decoded the text</span><br />
cout&lt;&lt;decoded&lt;&lt;endl;<br />
<span class="br0">&#125;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/huffman-encoding-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is Heap sort and how does it work? Heap sort algorithm</title>
		<link>http://www.datastructures.info/what-is-heap-sort-and-how-does-it-work-heap-sort-algorithm/</link>
		<comments>http://www.datastructures.info/what-is-heap-sort-and-how-does-it-work-heap-sort-algorithm/#comments</comments>
		<pubDate>Wed, 17 Jan 2007 16:47:00 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-heap-sort-and-how-does-it-work-heap-sort-algorithm/</guid>
		<description><![CDATA[
Heapsort is one of the best general-purpose sorting algorithms, a comparison sort and part of the selection sort family.
Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantages of worst-case O(n log n) runtime. The primary advantage of the heap sort is its efficiency. The execution time [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="What is Heap sort and how does it work? Heap sort algorithm animation" id="image33" src="http://www.datastructures.info/wp-content/uploads/2007/01/sorting_heapsort_anim.gif" /></p>
<p>Heapsort is one of the best general-purpose sorting algorithms, a comparison sort and part of the selection sort family.<a id="more-34"></a></p>
<p>Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantages of worst-case O(n log n) runtime. The primary advantage of the heap sort is its efficiency. The execution time efficiency of the heap sort is O(n log n). The memory efficiency of the heap sort, unlike the other O(nlogn) sorts, is constant, O(1), because the heap sort algorithm is not recursive. Heapsort is an in-place algorithm and is not a stable sort.</p>
<p><!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> A<span class="br0">&#91;</span><span class="nu0">11</span><span class="br0">&#93;</span>,j,item,temp;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">1</span>;i&lt;=<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
cin &gt;&gt; A<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> k=<span class="nu0">10</span>;k&gt;<span class="nu0">0</span>;k&#8211;<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">1</span>;i&lt;=k;i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
item=A<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
j=i/<span class="nu0">2</span>;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>j&gt;<span class="nu0">0</span> &amp;&amp; A<span class="br0">&#91;</span>j<span class="br0">&#93;</span>&lt;item<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
A<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=A<span class="br0">&#91;</span>j<span class="br0">&#93;</span>;<br />
i=j;<br />
j=j/<span class="nu0">2</span>;<br />
<span class="br0">&#125;</span><br />
A<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=item;<br />
<span class="br0">&#125;</span></p>
<p>temp=A<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
A<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=A<span class="br0">&#91;</span>k<span class="br0">&#93;</span>;<br />
A<span class="br0">&#91;</span>k<span class="br0">&#93;</span>=temp;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">1</span>;i&lt;=<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; A<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-heap-sort-and-how-does-it-work-heap-sort-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is QuickSort and how does it work? Video of cards sorted by Quick sort</title>
		<link>http://www.datastructures.info/what-is-quicksort-and-how-does-it-work-quick-sort-algorithm/</link>
		<comments>http://www.datastructures.info/what-is-quicksort-and-how-does-it-work-quick-sort-algorithm/#comments</comments>
		<pubDate>Wed, 17 Jan 2007 12:00:09 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Video</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-quicksort-and-how-does-it-work-quick-sort-algorithm/</guid>
		<description><![CDATA[
Pick an element from the array (the pivot), partition the remaining elements into those greater than and less than this pivot, and recursively sort the partitions. There are many variants of the basic scheme above: to select the pivot, to partition the array, to stop the recursion on small partitions, etc.
Quick-sort is a randomized sorting [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="What is QuickSort and how does it work? Quick Sort algorithm" id="image31" src="http://www.datastructures.info/wp-content/uploads/2007/01/sorting_quicksort_anim.gif" /></p>
<p>Pick an element from the array (the pivot), partition the remaining elements into those greater than and less than this pivot, and recursively sort the partitions. There are many variants of the basic scheme above: to select the pivot, to partition the array, to stop the recursion on small partitions, etc.<a id="more-32"></a></p>
<p>Quick-sort is a randomized sorting algorithm based on the divide-and-conquer method.</p>
<p>The worst-case efficiency of the quick sort, O(n2), occurs when the list is sorted and the left-most element is chosen. Randomly choosing a pivot point rather than using the left-most element is recommended if the data to be sorted isn&#8217;t random. As long as the pivot point is chosen randomly, the quick sort has an algorithmic complexity of O(nlogn).</p>
<p>Video of how quick sort works with cards:<br />
<ins><div class='yourTubeVideo_link'><a href='http://www.youtube.com/watch?v=ZrZLLbNJHOs'>View This Video on You Tube</a></div><div class='yourTubeVideo_holder'><div style='height:350px;' class='yourTubeVideo'><object style='width:425px;height:350px' type='application/x-shockwave-flash' data='http://www.youtube.com/v/ZrZLLbNJHOs'><param name='movie' value='http://www.youtube.com/v/ZrZLLbNJHOs'/><param name='scale' value='noScale' /><param name='wmode' value='window'/><param name='salign' value='TL' /></object></div></div></ins></p>
<p>The quick sort is by far the fastest of the common sorting algorithms. It&#8217;s possible to write a special-purpose sorting algorithm that can beat the quick sort for some data sets, but for general-case sorting there isn&#8217;t anything faster. Quick sort is not stable. The worst-case space-complexity is O(N), but it can be limited to O(log(N)) if the code is modified.</p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> a<span class="br0">&#91;</span><span class="nu0">10</span><span class="br0">&#93;</span>;<br />
<span class="kw4">int</span> partition<span class="br0">&#40;</span><span class="kw4">int</span> left, <span class="kw4">int</span> right<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> swap<span class="br0">&#40;</span><span class="kw4">int</span> i, <span class="kw4">int</span> j<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> sort<span class="br0">&#40;</span><span class="kw4">int</span> i, <span class="kw4">int</span> j<span class="br0">&#41;</span>;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> i,j=<span class="nu0">0</span>,k=<span class="nu0">9</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>;i&lt;<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
cin &gt;&gt; a<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;</p>
<p>sort<span class="br0">&#40;</span>j,k<span class="br0">&#41;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>;i&lt;<span class="nu0">10</span>;i++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; a<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> sort<span class="br0">&#40;</span><span class="kw4">int</span> left, <span class="kw4">int</span> right<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> p;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>left&gt;=right<span class="br0">&#41;</span><br />
<span class="kw1">return</span>;<br />
p = partition<span class="br0">&#40;</span>left, right<span class="br0">&#41;</span>;</p>
<p>sort<span class="br0">&#40;</span>left,p-<span class="nu0">1</span><span class="br0">&#41;</span>;<br />
sort<span class="br0">&#40;</span>p+<span class="nu0">1</span>,right<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> partition<span class="br0">&#40;</span><span class="kw4">int</span> left, <span class="kw4">int</span> right<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> first=left, pivot=right&#8211;;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>left&lt;=right<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">while</span><span class="br0">&#40;</span>a<span class="br0">&#91;</span>left<span class="br0">&#93;</span>&lt;a<span class="br0">&#91;</span>pivot<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
left++;<br />
<span class="kw1">while</span><span class="br0">&#40;</span><span class="br0">&#40;</span>right&gt;=first<span class="br0">&#41;</span>&amp;&amp;<span class="br0">&#40;</span>a<span class="br0">&#91;</span>right<span class="br0">&#93;</span>&gt;=a<span class="br0">&#91;</span>pivot<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
right&#8211;;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>left&lt;right<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
swap<span class="br0">&#40;</span>left,right<span class="br0">&#41;</span>;<br />
left++;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>left!=pivot<span class="br0">&#41;</span><br />
swap<span class="br0">&#40;</span>left,pivot<span class="br0">&#41;</span>;</p>
<p><span class="kw1">return</span> left;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> swap<span class="br0">&#40;</span><span class="kw4">int</span> i, <span class="kw4">int</span> j<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> temp=a<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
a<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=a<span class="br0">&#91;</span>j<span class="br0">&#93;</span>;<br />
a<span class="br0">&#91;</span>j<span class="br0">&#93;</span>=temp;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-quicksort-and-how-does-it-work-quick-sort-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is Selection sort and how does it work? Selection sort algorithm</title>
		<link>http://www.datastructures.info/what-is-selection-sort-and-how-does-it-work-selection-sort-algorithm/</link>
		<comments>http://www.datastructures.info/what-is-selection-sort-and-how-does-it-work-selection-sort-algorithm/#comments</comments>
		<pubDate>Tue, 16 Jan 2007 21:13:31 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-selection-sort-and-how-does-it-work-selection-sort-algorithm/</guid>
		<description><![CDATA[
Selection sort performs sorting by repeatedly putting the largest element in the unprocessed portion of the array to the end of the this unprocessed portion until the whole array is sorted.
In a loop, the highest element from the unsorted zone is selected (hence the name Selection Sort) and placed at the end of the unsorted [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="Selection sort in C++, What is selection sort?" id="image29" src="http://www.datastructures.info/wp-content/uploads/2007/01/selection_sort.gif" /></p>
<p>Selection sort performs sorting by repeatedly putting the largest element in the unprocessed portion of the array to the end of the this unprocessed portion until the whole array is sorted.<a id="more-30"></a></p>
<p>In a loop, the highest element from the unsorted zone is selected (hence the name Selection Sort) and placed at the end of the unsorted zone. We begin by selecting the largest element and moving it to the highest index position. We can do this by swapping the element at the highest index and the largest element.</p>
<p>The run time is Θ(n²), where n is the number of elements. The number of swaps is O(n).</p>
<p><!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> array<span class="br0">&#91;</span><span class="nu0">9</span><span class="br0">&#93;</span>=<span class="br0">&#123;</span><span class="nu0">4</span>,<span class="nu0">3</span>,<span class="nu0">5</span>,<span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">0</span>,<span class="nu0">7</span>,<span class="nu0">9</span>,<span class="nu0">4</span><span class="br0">&#125;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">9</span>; i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> min = i;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=i; j&lt;<span class="nu0">9</span>; j++<span class="br0">&#41;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>array<span class="br0">&#91;</span>min<span class="br0">&#93;</span>&lt;array<span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
min = j;</p>
<p>swap<span class="br0">&#40;</span>array<span class="br0">&#91;</span>min<span class="br0">&#93;</span>,array<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">9</span>;i++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; array<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-selection-sort-and-how-does-it-work-selection-sort-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Towers of Hanoi</title>
		<link>http://www.datastructures.info/the-towers-of-hanoi/</link>
		<comments>http://www.datastructures.info/the-towers-of-hanoi/#comments</comments>
		<pubDate>Fri, 12 Jan 2007 15:15:59 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-towers-of-hanoi/</guid>
		<description><![CDATA[
In an ancient city in India, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk.
And, there [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="The Towers of Hanoi" id="image25" src="http://www.datastructures.info/wp-content/uploads/2007/01/towers_of_hanoi.gif" /></p>
<p>In an ancient city in India, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk.<a id="more-26"></a></p>
<p>And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there. So, the monks start moving disks back and forth, between the original pile, the pile at the new location, and the intermediate location, always keeping the piles in order (largest on the bottom, smallest on the top). The legend is that, before the monks make the final move to complete the new pile in the new location, the temple will turn to dust and the world will end. Is there any truth to this legend?</p>
<p>The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms. The goal is to move all the discs from the left peg to the right one, obeying the following rules:</p>
<ol>
<li>Only one disk may be moved at a time.</li>
<li>Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.</li>
<li>No disk may be placed on top of a smaller disk.</li>
</ol>
<p>There are many recursive solutions to this problem, here are two non-recursive solutions. One is by Prasad Krishna and the other one I found somewhere on the web, some time ago (sorry for not crediting you, please leave a comment if it is your code <img src='http://www.datastructures.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )<br />
<!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;<br />
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> n, x;</p>
<p><a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&#8220;How many disks?&#8221;</span> &lt;&lt; endl;<br />
cin &gt;&gt; n;</p>
<p><span class="kw1">for</span> <span class="br0">&#40;</span>x=<span class="nu0">1</span>; x &lt; <span class="br0">&#40;</span><span class="nu0">1</span> &lt;&lt; n<span class="br0">&#41;</span>; x++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&#8220;Move from pole &#8220;</span> &lt;&lt; <span class="br0">&#40;</span>x&amp;x-<span class="nu0">1</span><span class="br0">&#41;</span>%<span class="nu0">3</span> &lt;&lt; <span class="st0">&#8221; to pole &#8220;</span> &lt;&lt; <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="br0">&#40;</span>x|x-<span class="nu0">1</span><span class="br0">&#41;</span>+<span class="nu0">1</span><span class="br0">&#41;</span>%<span class="nu0">3</span><span class="br0">&#41;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> st<span class="br0">&#91;</span><span class="nu0">20</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>,top,o=<span class="nu0">1</span>,m=<span class="nu0">2</span>,d=<span class="nu0">3</span>,n, i,j,k;<br />
top=<span class="nu0">0</span>;<br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&#8220;How many disks?&#8221;</span> &lt;&lt; endl;<br />
cin &gt;&gt; n;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>=o;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=d;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>=m;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>=n;</p>
<p><span class="kw1">while</span><span class="br0">&#40;</span>top&gt;=<span class="nu0">0</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
i=st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;<br />
j=st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
k=st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;<br />
n=st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>;</p>
<p>top=top-<span class="nu0">1</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>n&lt;=<span class="nu0">1</span><span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&#8220;Move &#8220;</span> &lt;&lt; i &lt;&lt; <span class="st0">&#8221; to &#8220;</span> &lt;&lt; j &lt;&lt; endl;<br />
<span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span>n&gt;<span class="nu0">1</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
top=top+<span class="nu0">1</span>;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>=k;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=j;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>=i;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>=n-<span class="nu0">1</span>;</p>
<p>top=top+<span class="nu0">1</span>;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>=i;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=j;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>=<span class="nu0">0</span>;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>=<span class="nu0">0</span>;<br />
top=top+<span class="nu0">1</span>;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>=i;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=k;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>=j;<br />
st<span class="br0">&#91;</span>top<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>=n-<span class="nu0">1</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-towers-of-hanoi/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Insertion sort algorithm</title>
		<link>http://www.datastructures.info/the-insertion-sort-algorithm/</link>
		<comments>http://www.datastructures.info/the-insertion-sort-algorithm/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 19:19:45 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-insertion-sort-algorithm/</guid>
		<description><![CDATA[Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted. It inserts each element of the array into its proper position.
Run time is O(n^2) because of moves. Insertion sort is an example of an incremental algorithm; it builds the sorted [...]]]></description>
			<content:encoded><![CDATA[<p>Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted. It inserts each element of the array into its proper position.<a id="more-24"></a></p>
<p>Run time is O(n^2) because of moves. Insertion sort is an example of an incremental algorithm; it builds the sorted sequence one number at a time. Insertion sort is stable, i.e. the relative order of equal keys is not changed, provided that you are careful about scanning the sorted region from right to left.</p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> array<span class="br0">&#91;</span><span class="nu0">9</span><span class="br0">&#93;</span>=<span class="br0">&#123;</span><span class="nu0">4</span>,<span class="nu0">3</span>,<span class="nu0">5</span>,<span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">0</span>,<span class="nu0">7</span>,<span class="nu0">9</span>,<span class="nu0">4</span><span class="br0">&#125;</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">1</span>; i&lt;<span class="nu0">9</span>; i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> index = array<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
<span class="kw4">int</span> dec = i;<br />
<span class="kw1">while</span><span class="br0">&#40;</span>dec&gt;<span class="nu0">0</span> &amp;&amp; array<span class="br0">&#91;</span>dec-<span class="nu0">1</span><span class="br0">&#93;</span>&gt;=index<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
array<span class="br0">&#91;</span>dec<span class="br0">&#93;</span>=array<span class="br0">&#91;</span>dec-<span class="nu0">1</span><span class="br0">&#93;</span>;<br />
&#8211;dec;<br />
<span class="br0">&#125;</span><br />
array<span class="br0">&#91;</span>dec<span class="br0">&#93;</span>=index;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">9</span>;i++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; array<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-insertion-sort-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Backtracking algorithm/method</title>
		<link>http://www.datastructures.info/the-backtracking-algorithmmethod/</link>
		<comments>http://www.datastructures.info/the-backtracking-algorithmmethod/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 15:20:02 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-backtracking-algorithmmethod/</guid>
		<description><![CDATA[
Backtracking method tries to find a solution by trying one of several choices. If the choice proves incorrect, computation backtracks or restarts at the point of choice and tries another choice.
Scope: The most widespread use of backtracking is in the execution of regular expressions. For example, the simple pattern &#8220;a*a&#8221; will fail to match the [...]]]></description>
			<content:encoded><![CDATA[<p><img id="image23" alt="The Backtracking algorithm/method, MAZE" src="http://www.datastructures.info/wp-content/uploads/2007/01/backtracking.gif" /><br />
Backtracking method tries to find a solution by trying one of several choices. If the choice proves incorrect, computation backtracks or restarts at the point of choice and tries another choice.<a id="more-22"></a></p>
<p><strong><em><u>Scope:</u></em></strong> The most widespread use of backtracking is in the execution of regular expressions. For example, the simple pattern &#8220;a*a&#8221; will fail to match the sequence &#8220;a&#8221; without backtracking (because, on the first pass the &#8220;a&#8221; is eaten by the &#8220;a*&#8221; leaving nothing behind for the remaining &#8220;a&#8221; to match.) Another usage of backtracking is in text parsing applications, solving mazes, robotics and etc.</p>
<p><u><em><strong>Bicolorable graph:</strong></em></u> This program will check if a graph is bicolorable in such a way that no two adjacent nodes have the same color.</p>
<p><!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> G<span class="br0">&#91;</span><span class="nu0">50</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">50</span><span class="br0">&#93;</span>; <span class="co1">//adjacency matrix for the graph</span><br />
<span class="kw4">int</span> num_edges; <span class="co1">//number of edges</span><br />
<span class="kw4">int</span> num_nodes; <span class="co1">//number of nodes</span></p>
<p>bool diff_color<span class="br0">&#40;</span><span class="kw4">int</span> node, <span class="kw4">int</span> color, <span class="kw4">int</span> colored<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> v = <span class="nu0">0</span>; v &lt; node; v++<span class="br0">&#41;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>G<span class="br0">&#91;</span>v<span class="br0">&#93;</span><span class="br0">&#91;</span>node<span class="br0">&#93;</span> &amp;&amp; colored<span class="br0">&#91;</span>v<span class="br0">&#93;</span> == color<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//check if they have same color and</span><br />
<span class="kw1">return</span> <span class="kw2">false</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//if the edge exists between the two nodes.</span></p>
<p><span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></p>
<p>bool backtracking<span class="br0">&#40;</span><span class="kw4">int</span> colored<span class="br0">&#91;</span><span class="br0">&#93;</span>, <span class="kw4">int</span> node<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> color = <span class="nu0">0</span>; color &lt; <span class="nu0">2</span>; color++<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//try for both colors</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>diff_color<span class="br0">&#40;</span>node, color, colored<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
colored<span class="br0">&#91;</span>node<span class="br0">&#93;</span> = color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//set the color to the current vertice</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>node == num_nodes-<span class="nu0">1</span><span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//check if the current node == last node</span><br />
<span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="kw1">else</span><br />
<span class="kw1">return</span> backtracking<span class="br0">&#40;</span>colored, node+<span class="nu0">1</span><span class="br0">&#41;</span>;&nbsp; &nbsp; <span class="co1">//backtrack it again</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">return</span> <span class="kw2">false</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> u, v, counter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//input vertices that are connected</span><br />
<span class="kw4">int</span> colored<span class="br0">&#91;</span><span class="nu0">50</span><span class="br0">&#93;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//colored vertices,</span><br />
cin &gt;&gt; num_nodes &gt;&gt; num_edges;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; <span class="nu0">50</span>; i++<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//make all the vertices unconected by</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j = <span class="nu0">0</span>; j &lt; <span class="nu0">50</span>; j++<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//setting the matrix G[all][all]=0</span><br />
G<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#91;</span>j<span class="br0">&#93;</span> = <span class="nu0">0</span>;</p>
<p><span class="kw1">while</span><span class="br0">&#40;</span>num_edges&gt;<span class="nu0">0</span><span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//loop until all edges are entered</span><br />
<span class="br0">&#123;</span><br />
num_edges&#8211;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//num_edges one less</span><br />
cin &gt;&gt; u &gt;&gt; v;<br />
G<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="br0">&#91;</span>v<span class="br0">&#93;</span> = <span class="nu0">1</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//when an edge exists then put in the</span><br />
G<span class="br0">&#91;</span>v<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span> = <span class="nu0">1</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//adjecency matrix a 1</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>backtracking<span class="br0">&#40;</span>colored, <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//check if the graph is bicolorable</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt;&nbsp; <span class="st0">&#8220;The graph can be bicolored.&#8221;</span> &lt;&lt; endl;<br />
<span class="kw1">else</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt;&nbsp; <span class="st0">&#8220;The graph can not be bicolored.&#8221;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-backtracking-algorithmmethod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Las Vegas algorithm/method</title>
		<link>http://www.datastructures.info/the-las-vegas-algorithmmethod/</link>
		<comments>http://www.datastructures.info/the-las-vegas-algorithmmethod/#comments</comments>
		<pubDate>Wed, 10 Jan 2007 13:20:18 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-las-vegas-algorithmmethod/</guid>
		<description><![CDATA[
A Las Vegas algorithm is a randomized algorithm that always gives the correct answer. Because of its nondeterministic nature, the run-time of a Las Vegas algorithm is a random variable.
An example of such an algorithm would be the randomized quickSort algorithm, it takes a random pivot point but at the end you get sorted data.
Scope: [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="The Las Vegas algorithm/method" id="image19" src="http://www.datastructures.info/wp-content/uploads/2007/01/las_vegas.jpg" /></p>
<p>A Las Vegas algorithm is a randomized algorithm that always gives the correct answer. Because of its nondeterministic nature, the run-time of a Las Vegas algorithm is a random variable.<a id="more-20"></a></p>
<p>An example of such an algorithm would be the randomized quickSort algorithm, it takes a random pivot point but at the end you get sorted data.</p>
<p><u><em><strong>Scope:</strong></em></u> Las Vegas algorithms are used to solve some of NP Complete problems, Genetic algorithms, Evolution Strategies, Ant Colony Optimization and etc. The time to solve these is random, which means the duration shouldn’t be very big/long so another application of it is Cryptography application, generation of very long prime numbers and etc.</p>
<p><u><em><strong>Miller-Rabin Primality test:</strong></em></u> An algorithm which determines whether a given number is prime, similar to the Fermat primality test and the Solovay-Strassen primality test.</p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
<span class="co2">#include &lt;ctime&gt;</span><br />
using namespace std;<br />
<span class="kw4">int</span> RandInt<span class="br0">&#40;</span><span class="kw4">int</span> Low, <span class="kw4">int</span> High<span class="br0">&#41;</span>        <span class="co1">//routine to return a random number between</span><br />
<span class="br0">&#123;</span>                    <span class="co1">//low and high boundaries</span><br />
<span class="kw1">return</span> rand<span class="br0">&#40;</span> <span class="br0">&#41;</span> % <span class="br0">&#40;</span> High - Low + <span class="nu0">1</span> <span class="br0">&#41;</span> + Low;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> Witness<span class="br0">&#40;</span><span class="kw4">int</span> A, <span class="kw4">int</span> i, <span class="kw4">int</span> N<span class="br0">&#41;</span>         <span class="co1">//if witness does not return 1 then N is</span><br />
<span class="br0">&#123;</span>                                            <span class="co1">//definately composite. Fermat&#8217;s little</span><br />
<span class="kw4">int</span> X, Y;                                 <span class="co1">//theorem, says that if n is prime, then</span><br />
<span class="co1">//for all a between 1 and n - 1, an - 1 mod n = 1.</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span> i == <span class="nu0">0</span> <span class="br0">&#41;</span>                              <span class="co1">//the contrapositive is if an - 1 mod n ? 1,</span><br />
<span class="kw1">return</span> <span class="nu0">1</span>;                              <span class="co1">//then n is not prime. Take a randomly!</span></p>
<p>X = Witness<span class="br0">&#40;</span> A, i / <span class="nu0">2</span>, N <span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span> X == <span class="nu0">0</span> <span class="br0">&#41;</span>                              <span class="co1">// if N is recursively composite, stop</span><br />
<span class="kw1">return</span> <span class="nu0">0</span>;</p>
<p>Y = <span class="br0">&#40;</span> X * X <span class="br0">&#41;</span> % N;                    <span class="co1">// N is not prime if we find a non-trivial root of 1</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span> Y == <span class="nu0">1</span> &amp;&amp; X != <span class="nu0">1</span> &amp;&amp; X != N - <span class="nu0">1</span> <span class="br0">&#41;</span><br />
<span class="kw1">return</span> <span class="nu0">0</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span> i % <span class="nu0">2</span> != <span class="nu0">0</span> <span class="br0">&#41;</span><br />
Y = <span class="br0">&#40;</span> A * Y <span class="br0">&#41;</span> % N;<br />
<span class="kw1">return</span> Y;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> CheckPrime<span class="br0">&#40;</span> <span class="kw4">int</span> N <span class="br0">&#41;</span>                 <span class="co1">//repeat this procedure as many times as</span><br />
<span class="br0">&#123;</span>                                           <span class="co1">//needed for desired error rate. Test if N &gt;= 3 is</span><br />
<span class="kw1">return</span> Witness<span class="br0">&#40;</span>RandInt<span class="br0">&#40;</span><span class="nu0">2</span>, N-<span class="nu0">2</span><span class="br0">&#41;</span>, N-<span class="nu0">1</span>, N<span class="br0">&#41;</span> == <span class="nu0">1</span>;    <span class="co1">//prime using one value of A</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
srand<span class="br0">&#40;</span>time<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;                <span class="co1">//define random numbers</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">101</span>; i&lt;<span class="nu0">200</span>; i+= <span class="nu0">2</span><span class="br0">&#41;</span>            <span class="co1">//check primality of a few numbers</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>CheckPrime<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#41;</span>                <span class="co1">//if number is prime print it</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt;  i &lt;&lt; <span class="st0">&#8221; is a prime number&#8221;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-las-vegas-algorithmmethod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Dynamic programming method</title>
		<link>http://www.datastructures.info/the-dynamic-programming-method/</link>
		<comments>http://www.datastructures.info/the-dynamic-programming-method/#comments</comments>
		<pubDate>Wed, 10 Jan 2007 13:01:33 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-dynamic-programming-method/</guid>
		<description><![CDATA[
It can be described as follows: solve each small problem once, saving their solution; use the solutions of small problems to obtain solutions to larger problems.
The difference between dynamic programming problems and divide and conquer problems is the fact that the sub problems are not independent.
Scope: Dynamic programming algorithms are mostly used for optimization problems. [...]]]></description>
			<content:encoded><![CDATA[<p><img id="image17" alt="Fibonacci numbers, The Dynamic programming method" src="http://www.datastructures.info/wp-content/uploads/2007/01/fibrab.gif" /></p>
<p>It can be described as follows: solve each small problem once, saving their solution; use the solutions of small problems to obtain solutions to larger problems.<a id="more-18"></a></p>
<p>The difference between dynamic programming problems and divide and conquer problems is the fact that the sub problems are not independent.</p>
<p><strong><em><u>Scope:</u></em></strong> Dynamic programming algorithms are mostly used for optimization problems. For example the shortest path problem in a directed staged network, Neural networks, some economic applications. To be able to use dynamic programming, the problem must have certain properties:<br />
1) Simple sub problems: There must be a way to break the big problem into smaller sub problems. Sub problems must be identified with just a few indices.<br />
2) Sub problem optimization: An optimal solution to the big problem must always be a combination of optimal solutions to the sub problems.<br />
3) Sub problem overlap: Optimal solutions to unrelated problems can contain sub problems in common.</p>
<p><u><em><strong>Calculating Fibonacci numbers</strong></em></u>: After two starting values,each number is the sum of the two preceding numbers. For example:  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657&#8230;</p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> Fib<span class="br0">&#91;</span><span class="nu0">1000</span><span class="br0">&#93;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//define array for fib numbers</span><br />
Fib<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>=<span class="nu0">0</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//define the first two Fibonacci numbers</span><br />
Fib<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>=<span class="nu0">1</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">2</span>;i&lt;<span class="nu0">1000</span>;i++<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//get other Fibonacci numbers by adding the two preceding</span><br />
Fib<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=Fib<span class="br0">&#91;</span>i-<span class="nu0">2</span><span class="br0">&#93;</span>+Fib<span class="br0">&#91;</span>i-<span class="nu0">1</span><span class="br0">&#93;</span>;&nbsp; &nbsp; <span class="co1">//Fibonacci numbers</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-dynamic-programming-method/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Divide and Conquer algorithm/method</title>
		<link>http://www.datastructures.info/the-divide-and-conquer-algorithmmethod/</link>
		<comments>http://www.datastructures.info/the-divide-and-conquer-algorithmmethod/#comments</comments>
		<pubDate>Tue, 09 Jan 2007 21:17:53 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-divide-and-conquer-algorithmmethod/</guid>
		<description><![CDATA[The divide and conquer method works as follows, divide big problem into smaller sub problems; conquer each sub problem separately; merge the solutions of the sub problems into the solution of the big problem.
Usually, better running times are obtained when the size of the subproblems are roughly equal. To write a recursive algorithm, find how [...]]]></description>
			<content:encoded><![CDATA[<p>The divide and conquer method works as follows, divide big problem into smaller sub problems; conquer each sub problem separately; merge the solutions of the sub problems into the solution of the big problem.<a id="more-16"></a></p>
<p>Usually, better running times are obtained when the size of the subproblems are roughly equal. To write a recursive algorithm, find how the problem can be broken up into smaller problems of the same nature. Remember the base case!</p>
<p><u><em><strong>Scope:</strong></em></u> Divide and conquer is used to solve difficult problems, such as the classic Tower of Hanoi puzzle, sorting, calculation of the FFT(Fast Fourier Transformation), DSP(Digital Signal Processing) where you need parallelism for math calculations, other parallelism applications, to make efficient use of memory caches (the reason is that once a sub-problem is small enough, it and all its sub-problems can, in principle, be solved within the cache, without accessing the slower main memory).</p>
<p><u><strong><em>Fast power calculation:</em></strong></u> For example to calculate the power of some number normally we would multiply it each time with itself, so <em>a^5=a*a*a*a*a</em>. There is a much faster divide-and-conquer method, using the simple formula <em>a^n=a^(n/2) * a^(n/2)</em>. What makes this approach more efficient is that once we compute the first factor <em>a^(n/2)</em>, we can compute the second factor <em>a^(n/2)</em> using at most one more multiplication.<span style="font-size: 12pt; font-family: Arial" /></p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> FastPower<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> power<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//the extra fast power calculation routine</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>power==<span class="nu0">1</span><span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//if power equals 1 then return just a</span><br />
<span class="kw1">return</span> a;<br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> x=FastPower<span class="br0">&#40;</span>a,power/<span class="nu0">2</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//else call the same routine, on the divded power by 2</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>power%<span class="nu0">2</span><span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//if it is odd then return x*x*a</span><br />
<span class="kw1">return</span> x*x*a;<br />
<span class="kw1">else</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//else if it is even then return x*x</span><br />
<span class="kw1">return</span> x*x;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
cout&lt;&lt; FastPower<span class="br0">&#40;</span><span class="nu0">2</span>,<span class="nu0">11</span><span class="br0">&#41;</span> &lt;&lt; endl;&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//call the routine for the extra fast power</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-divide-and-conquer-algorithmmethod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Monte Carlo algorithm/method</title>
		<link>http://www.datastructures.info/the-monte-carlo-algorithmmethod/</link>
		<comments>http://www.datastructures.info/the-monte-carlo-algorithmmethod/#comments</comments>
		<pubDate>Tue, 09 Jan 2007 20:55:50 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-monte-carlo-algorithmmethod/</guid>
		<description><![CDATA[The Monte Carlo method is a way of solving problems using statistical methods; stochastic technique (which means using random numbers) and probability.
Given some probability, P, that an event will occur in certain conditions, a computer can be used to generate those conditions repeatedly. The number of times the event occurs divided by the number of [...]]]></description>
			<content:encoded><![CDATA[<p>The Monte Carlo method is a way of solving problems using statistical methods; stochastic technique (which means using random numbers) and probability.<a id="more-15"></a></p>
<p>Given some probability, P, that an event will occur in certain conditions, a computer can be used to generate those conditions repeatedly. The number of times the event occurs divided by the number of times the conditions are generated should be approximately equal to P.</p>
<p><em><strong><u>Scope:</u></strong></em> The Monte Carlo methods are applied in various fields of mathematics, physics, economics and etc. Monte Carlo simulation methods are especially useful in studying systems with a large number of coupled degrees of freedom, such as liquids, disordered materials, strongly coupled solids, and cellular structures. The Monte Carlo method is often useful for solving problems in physics and mathematics which cannot be solved by analytical means. They are useful for modeling phenomena with significant uncertainty in inputs, such as the calculation of risk in business.</p>
<p><u><em><strong>Calculation of Pi:</strong></em></u> Let’s make a circle inside a square and let’s take a quadrant out of it.</p>
<p align="center"><img id="image14" alt="Calculation of Pi, The Monte Carlo algorithm/method" src="http://www.datastructures.info/wp-content/uploads/2007/01/calculation_of_pi.gif" /></p>
<p align="left">If we take a dice and throw it randomly inside the square, the probability that the dice will end up in the circle area is <em>circle_area/square_area</em>. Now if we replace it with formulas, just for one quadrant, we get: <em>Pi=4*dice_hitting_circle_area/dice_hitting_square_area</em>. So by Pythagoras theorem we just have to take random values for x and y coordinates, square them, sum them up, take the square root out of the sum and if the result is less or equal to 1 then the dice is inside the circle. By repeating this few times we can get the approximation of the number Pi.</p>
<p align="left"><!--adsense--></p>
<p><img id="image11" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img id="image12" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
<span class="co2">#include &lt;ctime&gt;</span><br />
<span class="co2">#include &lt;cmath&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">double</span> x, y;            <span class="co1">//define x and y coordiantes</span><br />
<span class="kw4">int</span> count_inside = <span class="nu0">0</span>, till=<span class="nu0">1000</span>;    <span class="co1">//how many tries you want</span><br />
srand<span class="br0">&#40;</span>time<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;           <span class="co1">//define random values</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;till; i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
x=pow<span class="br0">&#40;</span><span class="kw4">double</span><span class="br0">&#40;</span>rand<span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">1000</span><span class="br0">&#41;</span>/<span class="nu0">1000</span>,<span class="nu0">2</span><span class="br0">&#41;</span>;    <span class="co1">//square random x, x^2</span><br />
y=pow<span class="br0">&#40;</span><span class="kw4">double</span><span class="br0">&#40;</span>rand<span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">1000</span><span class="br0">&#41;</span>/<span class="nu0">1000</span>,<span class="nu0">2</span><span class="br0">&#41;</span>;    <span class="co1">//square random y, y^2</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>sqrt<span class="br0">&#40;</span>x+y<span class="br0">&#41;</span>&lt;=<span class="nu0">1</span><span class="br0">&#41;</span>                <span class="co1">//check if square root is equal or less to 1</span><br />
count_inside++;                <span class="co1">//if it is then add 1 to count_inside</span><br />
<span class="br0">&#125;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="nu0">4</span>*<span class="kw4">double</span><span class="br0">&#40;</span>count_inside<span class="br0">&#41;</span>/till &lt;&lt; endl;    <span class="co1">//calculate the pi by the formula given above</span><br />
<span class="br0">&#125;</span><br />
out &lt;&lt; <span class="nu0">4</span>*<span class="kw4">double</span><span class="br0">&#40;</span>count_inside<span class="br0">&#41;</span>/till &lt;&lt; endl; <span class="co1">//calculate the pi by the formula given above</span><br />
<span class="br0">&#125;</span></div>
</div>
<p> <!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-monte-carlo-algorithmmethod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Greedy algorithm/method</title>
		<link>http://www.datastructures.info/the-greedy-algorithm/</link>
		<comments>http://www.datastructures.info/the-greedy-algorithm/#comments</comments>
		<pubDate>Tue, 09 Jan 2007 19:36:34 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/the-greedy-algorithm/</guid>
		<description><![CDATA[A greedy algorithm repeatedly executes a procedure which tries to maximize the return based on examining local conditions, with the hope that the outcome will lead to a desired outcome for the global problem.
In some cases such a strategy is guaranteed to offer optimal solutions, and in some other cases it may provide a compromise [...]]]></description>
			<content:encoded><![CDATA[<p>A greedy algorithm repeatedly executes a procedure which tries to maximize the return based on examining local conditions, with the hope that the outcome will lead to a desired outcome for the global problem.<a id="more-13"></a></p>
<p>In some cases such a strategy is guaranteed to offer optimal solutions, and in some other cases it may provide a compromise that produces acceptable approximations. Typically, the greedy algorithms employ simple strategies that are simple to implement and require minimal amount of resources. For most optimization problems, greedy algorithms are not optimal. But when they are, they are usually the fastest available.</p>
<p><strong><em><u>Scope:</u></em></strong> Greedy algorithms do not always yield a genuinely optimal solution. In such cases the greedy method is frequently the basis of a heuristic approach. Even for problems which can be solved exactly by a greedy algorithm, establishing the correctness of the method may be a non-trivial process. Greedy approximation algorithms have been frequently used to obtain sparse solutions to learning problems. For example, all known greedy algorithms for the graph coloring problem and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum.</p>
<p><u><em><strong>Egyptian fractions:</strong></em></u> The ancient Egyptians only used fractions of the form <sup>1</sup>/<sub>n</sub> so any other fraction had to be represented as a <em>sum of such unit fractions</em> and, furthermore, all the unit fractions were different! An example:</p>
<p><sup>6</sup>/<sub>7</sub> = <sup>1</sup>/<sub>2</sub> + <sup>1</sup>/<sub>3</sub> + <sup>1</sup>/<sub>42</sub></p>
<p>This problem is solved using the greedy method.</p>
<p><!--adsense--></p>
<p><img title="C++ Code" alt="C++ Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img title="C Code" alt="C Code" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> numerator,denominator, i=<span class="nu0">1</span>;&nbsp; &nbsp; <span class="co1">//define the numerator and denominator</span><br />
<span class="kw4">double</span> factor,temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//define the factor for division num/den</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; “Enter numerator and then denominator” &lt;&lt; endl;<br />
cin&gt;&gt;numerator&gt;&gt;denominator;<br />
factor=<span class="kw4">double</span><span class="br0">&#40;</span>numerator<span class="br0">&#41;</span>/<span class="kw4">double</span><span class="br0">&#40;</span>denominator<span class="br0">&#41;</span>;&nbsp; &nbsp; <span class="co1">//divide the factor</span><br />
<span class="kw1">do</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//and put it in temp</span><br />
<span class="br0">&#123;</span><br />
temp=<span class="kw4">double</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>/<span class="kw4">double</span><span class="br0">&#40;</span>++i<span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//result value of the fraction</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span>factor&gt;=temp<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//if factor is less than temp then subtract</span><br />
<span class="br0">&#123;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//temp from factor and then print the factor</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; i &lt;&lt; <span class="st0">&#8221; &#8220;</span>;<br />
factor = factor - temp;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="kw1">while</span><span class="br0">&#40;</span>factor&gt;<span class="nu0">0</span> &amp;&amp; i&lt;<span class="nu0">32000</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//check for all factors till the denominator is</span><br />
<span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//32000 and as long as it is greater than 0 </span><br />
&nbsp;</div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/the-greedy-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is Bubble sort and how does it work?</title>
		<link>http://www.datastructures.info/what-is-bubble-sort-and-how-does-it-work/</link>
		<comments>http://www.datastructures.info/what-is-bubble-sort-and-how-does-it-work/#comments</comments>
		<pubDate>Sat, 09 Dec 2006 21:49:37 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Resources</category>

		<category>Algorithms</category>

		<category>Code</category>

		<category>Video</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-bubble-sort-and-how-does-it-work/</guid>
		<description><![CDATA[Bubble sort, sometimes shortened to bubblesort, also known as exchange sort, is a simple sorting algorithm. 
It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, [...]]]></description>
			<content:encoded><![CDATA[<p>Bubble sort, sometimes shortened to bubblesort, also known as exchange sort, is a simple sorting algorithm. <a id="more-10"></a></p>
<p>It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements &#8220;bubble&#8221; to the top (i.e. the beginning) of the list via the swaps. Because it only uses comparisons to operate on elements, it is a comparison sort.</p>
<p>Video of how bubble sort works:<br />
<ins><div class='yourTubeVideo_link'><a href='http://www.youtube.com/watch?v=t_xkgcakREw'>View This Video on You Tube</a></div><div class='yourTubeVideo_holder'><div style='height:350px;' class='yourTubeVideo'><object style='width:425px;height:350px' type='application/x-shockwave-flash' data='http://www.youtube.com/v/t_xkgcakREw'><param name='movie' value='http://www.youtube.com/v/t_xkgcakREw'/><param name='scale' value='noScale' /><param name='wmode' value='window'/><param name='salign' value='TL' /></object></div></div></ins></p>
<p><!--adsense--></p>
<p><img alt="C++ Code" id="image11" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_cplusplus.png" /><img alt="C Code" id="image12" src="http://www.datastructures.info/wp-content/uploads/2006/12/page_white_c.png" /></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">#include &lt;iostream&gt;</span><br />
using namespace std;</p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> array<span class="br0">&#91;</span><span class="nu0">9</span><span class="br0">&#93;</span>=<span class="br0">&#123;</span><span class="nu0">4</span>,<span class="nu0">3</span>,<span class="nu0">5</span>,<span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">0</span>,<span class="nu0">7</span>,<span class="nu0">9</span>,<span class="nu0">4</span><span class="br0">&#125;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">9</span>; i++<span class="br0">&#41;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> j=<span class="nu0">0</span>; j<br />
<span class="kw1">if</span><span class="br0">&#40;</span>array<span class="br0">&#91;</span>i<span class="br0">&#93;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw4">int</span> temp = array<span class="br0">&#91;</span>j<span class="br0">&#93;</span>;<br />
array<span class="br0">&#91;</span>j<span class="br0">&#93;</span>= array<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
array<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=temp;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">9</span>;i++<span class="br0">&#41;</span><br />
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; array<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span></div>
</div>
<p><!--adsense#link-->
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-bubble-sort-and-how-does-it-work/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is sorting?</title>
		<link>http://www.datastructures.info/what-is-sorting/</link>
		<comments>http://www.datastructures.info/what-is-sorting/#comments</comments>
		<pubDate>Sat, 09 Dec 2006 21:37:49 +0000</pubDate>
		<dc:creator>refikh</dc:creator>
		
		<category>Algorithms</category>

		<category>Explanation</category>

		<guid isPermaLink="false">http://www.datastructures.info/what-is-sorting/</guid>
		<description><![CDATA[
In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order.
Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it [...]]]></description>
			<content:encoded><![CDATA[<p><img id="image8" alt="Sorting algorithm, What is sorting?" src="http://www.datastructures.info/wp-content/uploads/2006/12/sorting.jpg" /></p>
<p>In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order.<a id="more-9"></a></p>
<p>Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:</p>
<ol>
<li>The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);</li>
<li>The output is a permutation, or reordering, of the input.</li>
</ol>
<p>Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956. Although many consider it a solved problem, useful new sorting algorithms are still being invented to this day (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide-and-conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds.</p>
<p><!--adsense--></p>
<p>Taken from Wikipedia
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.datastructures.info/what-is-sorting/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
