<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ganesh's Weblog</title>
	<atom:link href="http://gaanesh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gaanesh.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Wed, 10 Nov 2010 04:22:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gaanesh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ganesh's Weblog</title>
		<link>http://gaanesh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gaanesh.wordpress.com/osd.xml" title="Ganesh&#039;s Weblog" />
	<atom:link rel='hub' href='http://gaanesh.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to change the default crontab editor value</title>
		<link>http://gaanesh.wordpress.com/2010/11/10/how-to-change-the-default-crontab-editor-value/</link>
		<comments>http://gaanesh.wordpress.com/2010/11/10/how-to-change-the-default-crontab-editor-value/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 04:22:59 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[unix tips]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=73</guid>
		<description><![CDATA[export EDITOR=vim<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=73&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>export EDITOR=vim</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=73&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2010/11/10/how-to-change-the-default-crontab-editor-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>23-02-2009</title>
		<link>http://gaanesh.wordpress.com/2009/02/27/23-02-2009/</link>
		<comments>http://gaanesh.wordpress.com/2009/02/27/23-02-2009/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 04:43:21 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=68</guid>
		<description><![CDATA[On 23rd Feb 2009 around 6 PM, Shaju went and asked divya whether she have any plans for her marriage. And the conversation goes like .. Divya: ella not now, yethuva erunthalum oru 6 maasam agatum. Shaju: Yen apathan Ganesh Indiaku varana ? Divya: (She admitted) yes, I wanted to marry Ganesh PS: dialogue are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=68&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On 23rd Feb 2009 around 6 PM, Shaju went and asked divya whether she have any plans for her marriage. And the conversation goes like ..</p>
<p>Divya: ella not now, yethuva erunthalum oru 6 maasam agatum.<br />
Shaju: Yen apathan Ganesh Indiaku varana ?<br />
Divya: (She admitted) yes, I wanted to marry Ganesh</p>
<p>PS: dialogue are all my own. But the matter is same <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>And after this, as usual her mom was started crying. Actually I feel very very bad for her. I like her a lot and I feel very sad that becoz of me she is crying today <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  . Sorry aunty.</p>
<p>Shaju was not happy with the way I handled the matter when we get caught last time. So this time he is not going to call me. </p>
<p>To be continued ..</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=68&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2009/02/27/23-02-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Customized &#8216;use lib&#8217; in perl</title>
		<link>http://gaanesh.wordpress.com/2009/02/04/use-lib-perl/</link>
		<comments>http://gaanesh.wordpress.com/2009/02/04/use-lib-perl/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 05:12:40 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=63</guid>
		<description><![CDATA[When you have restriction to install your perl module under c:\program file\perl, try to install it under your local directory. Suppose, your perl module is installed under C:\mydir\perlmodules\lib, then you should let your perl script to load/use your local perl module. # this will include the below directory for loading the modules. use lib "C:\mydir\perlmodules\lib";<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=63&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When you have restriction to install your perl module under c:\program file\perl, try to install it under your local directory. Suppose, your perl module is installed under C:\mydir\perlmodules\lib, then you should let your perl script to load/use your local perl module.</p>
<pre class="brush: c-sharp; highlight-lines: [2, 4]">
<code># this will include the below directory for loading the modules.
use lib "C:\mydir\perlmodules\lib";</code>
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=63&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2009/02/04/use-lib-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Bit count</title>
		<link>http://gaanesh.wordpress.com/2009/01/08/bit-count/</link>
		<comments>http://gaanesh.wordpress.com/2009/01/08/bit-count/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 07:22:07 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=61</guid>
		<description><![CDATA[Problem: Given a number, count the number of bits set in that number. There are 2 methods represented below. First solution does the calculation in constant time, but it assumes it is a 32-bit machine. Second solution doesnot assume anything such that and it works for both 32 &#38; 64-bit machine. But the runtime of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=61&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong><br />
Given a number, count the number of bits set in that number. There are 2 methods represented below.<br />
First solution does the calculation in constant time, but it assumes it is a 32-bit machine.<br />
Second solution doesnot assume anything such that and it works for both 32 &amp; 64-bit machine. But the runtime of that machine is O(M) where M is the number of bit set in that number.</p>
<p><strong>Soultion:</strong></p>
<pre class="brush: cpp;">
#include &lt;iostream.h&gt;
using namespace std;

typedef unsigned long long uint64;  //assume this gives 64-bits
unsigned long m1  = 0x5555555; //binary: 0101...
const uint64 m2  = 0x33333333; //binary: 00110011..
const uint64 m4  = 0x0f0f0f0f; //binary:  4 zeros,  4 ones ...
const uint64 m8  = 0x00ff00ff; //binary:  8 zeros,  8 ones ...
const uint64 m16 = 0x0000ffff; //binary: 16 zeros, 16 ones ...
const uint64 hff = 0xffffffff; //binary: all ones
const uint64 h01 = 0x01010101; //the sum of 256 to the power of 0,1,2,3...

//This is a naive implementation, shown for comparison,
//and to help in understanding the better functions.
//It uses 24 arithmetic operations (shift, add, and).
int popcount_1(uint64 x) {
    x = (x &amp; m1 ) + ((x &gt;&gt;  1) &amp; m1 ); //put count of each  2 bits into those  2 bits
    x = (x &amp; m2 ) + ((x &gt;&gt;  2) &amp; m2 ); //put count of each  4 bits into those  4 bits
    x = (x &amp; m4 ) + ((x &gt;&gt;  4) &amp; m4 ); //put count of each  8 bits into those  8 bits
    x = (x &amp; m8 ) + ((x &gt;&gt;  <img src='http://s2.wp.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> &amp; m8 ); //put count of each 16 bits into those 16 bits
    x = (x &amp; m16) + ((x &gt;&gt; 16) &amp; m16); //put count of each 32 bits into those 32 bits
    return x;
}

int countNaive(int number)
{
    int count = 0;
    while(number)
    {
        count++;
        number = number &amp; (number -1);
    }

    return count;
}
int main()
{
    cout&lt;&lt;&quot;Enter the number: &quot;;
    int number;
    cin&gt;&gt;number;
    cout&lt;&lt;&quot;Number of bits: &quot;&lt;&lt;popcount_1(number)&lt;&lt;endl;
    cout&lt;&lt;&quot;Number of bits(Naive Method): &quot;&lt;&lt;countNaive(number)&lt;&lt;endl;
    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=61&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2009/01/08/bit-count/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Arrange 0&#8242;s &amp; 1&#8242;s</title>
		<link>http://gaanesh.wordpress.com/2009/01/05/arrange-0s-1s/</link>
		<comments>http://gaanesh.wordpress.com/2009/01/05/arrange-0s-1s/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 08:32:35 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=59</guid>
		<description><![CDATA[Problem Statement: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; An array of size n among which there are n/2 0s and n/2 1s arranged in random order. Arrange all the 1s to left and all 0s to right but it needs to be stable i.e., the order of 1s and 0s in the i/p array should be maintained. Expected Complexity: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=59&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Problem Statement:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>An array of size n among which there are n/2 0s and n/2 1s arranged in random order. Arrange all the 1s to left and all 0s to right but it needs to be stable i.e., the order of 1s and 0s in the i/p array should be maintained.</p>
<p>Expected Complexity:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
 O(N) with constant space.</p>
<p>Pseudocode:<br />
&#8212;&#8212;&#8212;&#8211;<br />
1. Have two counters, count1 = 0 and count2 = (n/2)+1<br />
2. Traverse thru the array, if(arr[ i ] == 1) { arr[ i ] = count1++;} else { arr[ i ] = count2++ };</p>
<p>3) At the end of the traversal, you have array filled with numbers 0 to n-1</p>
<p>Now the problem reduces to sorting and array of n numbers which has elements from 0 to n-1 occuring only once which can be done in O(n).</p>
<pre class="brush: cpp;">

for(j = 0; j &lt;= 1; j++)
{
	for(i = 0; i&lt;n; i++)
	{
		if(arr[ i ] != i)
		{
			swap(arr[ i ], arr[ arr[ i ] ]);
		}
	}
}
</pre>
<p>Note: j loop runs only twice irrespective on &#8216;n&#8217; and has constant complexity. The order of this whole loop is 2*n = O(n).</p>
<p>4) After the array is sorted, Again traverse thru the array and make elements arr[0] to arr[n/2] to &#8217;1&#8242; and arr[(n/2)+1] to arr[n] as &#8217;0&#8242;.</p>
<p>Space complexity is constant and time complexity is O(step2) + O(step3) + O(step4) = n + 2n +n = 4*n = O(n).</p>
<p>Solution #1 (Stable):<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre class="brush: cpp;">
#include&lt;iostream.h&gt;
int a[] = { 1,0,0,0,1,1,1,0,0,1};
#define _SIZE sizeof(a)/sizeof(a[0])

/* Helper Function */
void printArray()
{
    for(int i=0;i&lt;_SIZE; ++i)
        cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;

    cout&lt;&lt;endl;
}

int main()
{
    int countOne = 0;
    int countZero = (_SIZE)/2;

    int i=0;

	/* Fill the array with numbers from 1 to N */
    for(i=0; i &lt; _SIZE; ++i)
    {
        if(a[i] == 1)
        {
            a[i] = countOne;
            countOne++;
        }
        else
        {
            a[i] = countZero;
            countZero++;
        }
    }
    printArray();

	/* Swap the number and make it a sorted one */
    for(int j = 0; j&lt; 2; ++j)
    {
        for(i=0; i &lt; _SIZE; ++i)
        {
            if(a[i] != i)
            {
                int temp = a[i];
                a[i] = a[a[i]];
                a[temp] = temp;
            }
        }
    }

    printArray();

	/* Fill the 1st N/2 elements with 1 */
    for(i=0; i&lt;_SIZE/2; ++i)
        a[i] = 1;

	/* Fill the last N/2 elements with 1 */
    for(i=_SIZE/2; i&lt;_SIZE; i++)
        a[i] = 0;

        printArray();

		return 0;
}
</pre>
<p>Solution #2 (UnStable):<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Here is a small variation of the quick sort to solve the same problem but this solution is not stable. So I prefer #1.</p>
<pre class="brush: cpp;">
#include&lt;iostream.h&gt;
using namespace std;
#define _SIZE sizeof(a)/sizeof(a[0])
void swap(int *a, int l, int r)
{
    cout&lt;&lt;&quot;Swaping &quot;&lt;&lt;l&lt;&lt;&quot; &quot;&lt;&lt;r&lt;&lt;endl;
    if(a[l] != a[r])
        a[l] ^= a[r] ^= a[l] ^= a[r];

    return;
}
bool compare_1_upper(int *a, int u, int p)
{
    return (a[u] == p);
}
bool compare_1_lower(int *a, int l, int p)
{
    return (a[l] &lt; p);
}

bool compare_0_upper(int *a, int u, int p)
{
    return (a[u] &gt; p);
}
bool compare_0_lower(int *a, int u, int p)
{
    return (a[u] == p);
}
bool (*funcPtr_upper)(int *, int, int) = 0;
bool (*funcPtr_lower)(int *, int, int) = 0;
void qPartition(int *a, int low, int upper)
{
    int pivot = a[low];
    int l = low - 1;
    int u = upper + 1;

    if(a[low])
    {
        funcPtr_upper = compare_1_upper;
        funcPtr_lower = compare_1_lower;
    }
    else
    {
        funcPtr_upper = compare_0_upper;
        funcPtr_lower = compare_0_lower;
    }

    while(1)
    {
        while(funcPtr_upper(a, --u, pivot));

        while(funcPtr_lower(a, ++l, pivot));

        if(l&lt;u)
            swap(a, l, u);
        else
            return;

    }
    return;

}

int a[] = { 1,1,1,1,1,0,0,1};

void print()
{
    for(int i=0; i&lt; _SIZE; ++i)
        cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;

    cout&lt;&lt;&quot;\n&quot;;
}

int main()
{
    print();
    qPartition(a, 0, _SIZE - 1);
    print();
    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=59&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2009/01/05/arrange-0s-1s/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Min/Max/Search in circular array</title>
		<link>http://gaanesh.wordpress.com/2008/12/31/minmaxsearch-in-circular-array/</link>
		<comments>http://gaanesh.wordpress.com/2008/12/31/minmaxsearch-in-circular-array/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 08:20:37 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=51</guid>
		<description><![CDATA[/* Problem: An array is shifted N times, so we get a circular shifted array. * 1. Search for a number in an efficient way * 2. Find the start index ie. MIN element in the array * 3. Find the end index ie. MAX element in the array */ #include&#60;iostream.h&#62; int array[] = { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=51&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp;">

/* Problem: An array is shifted N times, so we get a circular shifted array.
 * 1. Search for a number in an efficient way
 * 2. Find the start index ie. MIN element in the array
 * 3. Find the   end index ie. MAX element in the array
 */

#include&lt;iostream.h&gt;

int array[] = { 7,8,9,0,1,2,3,4,5,6};
#define SIZE sizeof(array)/sizeof(array[0])

void search(int key)
{
    int left = 0;
    int right = SIZE;
    int mid;
    while(left&lt;right)
    {
        if(right-left == 1)
        {
            if(array[left] == key)
                cout&lt;&lt;&quot;Match found\n&quot;;
            else if(array[right] == key)
                cout&lt;&lt;&quot;Match found\n&quot;;
            else
                cout&lt;&lt;&quot;Match not found\n&quot;;
            break;
        }
        mid = (right-left)/2 + left;

        if(array[mid] == key)
        {
            cout&lt;&lt;&quot;Key found: &quot;&lt;&lt;key&lt;&lt;endl;
            break;
        }
        if(array[left] &lt;= array[mid])
        {
            // sub case #1
            if(key &gt;= array[mid] &amp;&amp; array[right]&gt;=key)
            {
                left = mid;
            }
            else
                right = mid;
        }
        else
        {
            // right sub arrary is sorted
            if(key &gt;= array[mid] &amp;&amp; key &lt;= array[right])
                left = mid;
            else
                right = mid;
        }
    }

}
void findMax()
{
    int left = 0;
    int right = SIZE - 1;
    int mid;

    while(left&lt;right)
    {
        mid = (right-left) /2 + left;
        if(right-left == 1) {
            cout&lt;&lt;&quot;Max :&quot;&lt;&lt; (array[right] &gt; array[left] ? array[right]:array[left])&lt;&lt;endl;
            return;
        }
        if(array[mid] &gt; array[left] &amp;&amp; array[mid] &gt; array[right])
            left = mid;
        else if(array[mid] &lt; array[left] &amp;&amp; array[mid] &lt; array[right])
            right = mid;
        else
            left = mid;
    }

    cout&lt;&lt;&quot;Max element: &quot;&lt;&lt;array[mid] &lt;&lt;&quot; &quot;&lt;&lt;array[left]&lt;&lt;&quot; &quot;&lt;&lt;array[right]&lt;&lt;endl;
    return;
}

void findMin()
{
    int l = 0;
    int r = SIZE -1;
    int m;

    while(l&lt;r)
    {
        m = (r-l)/2+l;
        if(r-l == 1)
        {
            cout&lt;&lt;&quot;Min: &quot;&lt;&lt;(array[r]&lt;array[l] ? array[r]:array[l])&lt;&lt;&quot;\n&quot;;
            return;
        }
        if (array[l]  &gt; array[m] &amp;&amp; array[r] &gt; array[m])
            r = m;
        else if(array[l] &lt; array[m] &amp;&amp; array[r] &lt; array[m])
            l =  m;
        else
            r = m;
    }
    cout&lt;&lt;&quot;Minimum: &quot;&lt;&lt;array[m]&lt;&lt;&quot;\n&quot;;
}
int main()
{
    cout&lt;&lt;&quot;Enter the no you want to search!! &quot;;
    int key;
    cin&gt;&gt;key;

    search(key);
    findMax();
    findMin();
    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=51&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2008/12/31/minmaxsearch-in-circular-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>XOR Linked List</title>
		<link>http://gaanesh.wordpress.com/2008/12/31/xor-linked-list/</link>
		<comments>http://gaanesh.wordpress.com/2008/12/31/xor-linked-list/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 08:12:55 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=48</guid>
		<description><![CDATA[/* Problem Statement : * ------------------- * Implement a memory efficient Doubly Linked List * which means you are allowed to use only one pointer * to indicate both next and the previous pointers * * Soultion * --------- * The possible solution involves using a XOR Linked list compresses the same * information in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=48&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp;">

/* Problem Statement :
 * -------------------
 * Implement a memory efficient Doubly Linked List
 * which means you are allowed to use only one pointer
 * to indicate both next and the previous pointers
 *
 * Soultion
 * ---------
 * The possible solution involves using a XOR Linked list compresses the same
 * information in one address field by storing the bitwise XOR of the address
 * for the previous and the address for next in one field.
 * A B C D E ... &lt;-&gt; A⊕C &lt;-&gt; B⊕D &lt;-&gt; C⊕E &lt;-&gt;
 * When you traverse the list from left to right: supposing you are at C,
 * you can take the address of the previous item, B,
 * and XOR it with the value in the link field (B⊕D).
 * You will then have the address for D and you can continue traversing the list.
 * The same pattern applies in the other direction.
 *
 * Limitation:
 * ------------
 * 1. The list cannot be traversed from the middle unless we get the address of the previous element.
 * 2. General-purpose debugging tools cannot follow the XOR chain, making debugging more difficult.
 * 3. The price for the decrease in memory usage is an increase in code complexity, making maintenance more expensive;
 * 4. Most garbage collection schemes do not work with data structures that do not contain literal pointers;
 * 5.
 */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;assert.h&gt;

/* pointer stored the address of the node
 * since address is a mere number, we can store it happily
 */
typedef unsigned long pointer;

struct link
{
    pointer next_prev;
    int payload;
};
typedef struct link link;

link* add_data(int payload, struct link* Prevlist)
{
    struct link * new_link = (struct link*)malloc(sizeof(link));
    assert(new_link);

    /* Assigning the address of the previous element
     * in the current NODEs pointer
     */
    new_link-&gt;next_prev = (pointer)Prevlist;
    new_link-&gt;payload = payload;

    if (Prevlist != NULL)
    {
        /* If the previous Prevlist is not NULL, which means this is not the head
         * then XOR the Prevlist's previous node address with this New NODEs address
         * (ie) consider a list A&lt;-&gt;B&lt;-&gt;C&lt;-&gt;D
         *  we are creating a new node C.
         * B's next_prev would have A's address.
         * so while creating C, update (B-&gt;next_prev = A ^ C)
         */

        Prevlist-&gt;next_prev = (pointer) list-&gt;next_prev ^ (pointer)new_link;
    }
    return new_link;
}

void walk_list(link *list)
{
    struct link* prev = 0;
    while (list != NULL)
    {
        pointer next = ((pointer)prev) ^ list-&gt;next_prev;
        printf(&quot;%d &quot;, list-&gt;payload);
        prev = (struct link*)list;
        list = (link*)next;
    }
    printf(&quot;\n&quot;);
}

int main(void)
{
    /* Add the elements in the linked list
     * Remember you have to give the address of the previous node
     * while adding the current node
     */
    link *l1 = add_data(1, NULL);
    link *l2 = add_data(2, l1);
    link *l3 = add_data(3, l2);
    link *l4 = add_data(4, l3);
    link *l5 = add_data(5, l4);

    /* Walk from the head */
    walk_list(l1);

    /* Walk from the tail */
    walk_list(l5);

    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=48&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2008/12/31/xor-linked-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Reverse every Nth node in LL</title>
		<link>http://gaanesh.wordpress.com/2008/12/31/reverse-every-nth-node-in-ll/</link>
		<comments>http://gaanesh.wordpress.com/2008/12/31/reverse-every-nth-node-in-ll/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 07:53:53 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=46</guid>
		<description><![CDATA[/* Problem Statement : * Given a Linked List, reverse every Nth node in the Linked List */ #include&#60;iostream.h&#62; struct node { int data; struct node *next; }; typedef struct node NODE; NODE *form_LL(int limit) { NODE *head = NULL; for(int i = limit;i ; --i) { NODE *temp = new NODE; temp-&#62;data = i; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=46&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp;">

/* Problem Statement :
 * Given a Linked List, reverse every Nth node in the Linked List
 */
#include&lt;iostream.h&gt;

struct node
{
    int data;
    struct node *next;
};
typedef struct node NODE;

NODE *form_LL(int limit)
{
    NODE *head = NULL;
    for(int i = limit;i ; --i)
    {
        NODE *temp = new NODE;
        temp-&gt;data = i;
        temp-&gt;next = head;
        head = temp;
    }
    return head;
}
void print_LL(NODE *start)
{
    while(start)
    {
        cout&lt;&lt;start-&gt;data&lt;&lt;&quot; &quot;;
        start = start-&gt;next;
    }
    cout&lt;&lt;endl;
}
NODE *KReverse(NODE *head, int k)
{
    int count = 0;

    if(!head)
        return NULL;
    NODE *prev = NULL;
    NODE *current = head;
    NODE *next = NULL;
    while(current &amp;&amp; count &lt; k)
    {
        next = current-&gt;next;
        current-&gt;next = prev;
        prev = current;
        current = next;
        count++;
    }
    NODE *rest = KReverse(next, k);
    head-&gt;next = rest;
    return prev;
}

int main()
{
    int num;
    cout&lt;&lt;&quot;How many number of Nodes : &quot;;
    cin&gt;&gt;num;
    NODE *head = form_LL(num);
    print_LL(head);

    int k = 0;
    cout&lt;&lt;&quot;How many rotations : &quot;;
    cin&gt;&gt;k;
    NODE *new_node = KReverse(head, k);
    print_LL(new_node);
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=46&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2008/12/31/reverse-every-nth-node-in-ll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Convert BST to LL</title>
		<link>http://gaanesh.wordpress.com/2008/12/02/convert-bst-to-ll/</link>
		<comments>http://gaanesh.wordpress.com/2008/12/02/convert-bst-to-ll/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 09:59:23 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=31</guid>
		<description><![CDATA[/* Problem Statement : * Write a C code to conert a binary search tree to a linked list */ #include&#60;iostream.h&#62; #include&#60;queue.h&#62; struct node { int data; int newline; struct node *left; struct node *right; }; typedef struct node NODE; void insert_element(NODE **head, NODE *temp) { if(*head == NULL) { *head = temp; cout&#60;&#60;&#34;Head is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=31&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp;">

/* Problem Statement :
 * Write a C code to conert a binary search tree to a linked list
 */

#include&lt;iostream.h&gt;
#include&lt;queue.h&gt;

struct node {
    int data;
    int newline;
    struct node *left;
    struct node *right;
};
typedef struct node NODE;

void insert_element(NODE **head, NODE *temp)
{
    if(*head == NULL)
    {
        *head = temp;
        cout&lt;&lt;&quot;Head is : &quot;&lt;&lt;(*head)-&gt;data&lt;&lt;endl;
        return;
    }
    NODE *prev = NULL;
    NODE *current = *head;
    while(current)
    {
        prev = current;
        if(current-&gt;data &gt; temp-&gt;data)
        {
            current = current-&gt;left;
        }
        else
            current = current-&gt;right;
    }
    cout&lt;&lt;&quot;Inserting : &quot;&lt;&lt;temp-&gt;data&lt;&lt;endl;
    if(prev-&gt;data &gt; temp-&gt;data)
        prev-&gt;left = temp;
    else
        prev-&gt;right = temp;
}

NODE * form_BST(int *input, int size)
{
    NODE *head = NULL;
    NODE *temp = NULL;
    int count = 0;
    while(count &lt; size)
    {
        temp = new NODE;
        temp-&gt;data = input[count];
        temp-&gt;left = NULL;
        temp-&gt;right = NULL;
        temp-&gt;newline = 0;
        insert_element(&amp;head, temp);
        count++;
    }
    return head;
    cout&lt;&lt;&quot;end of BST\n&quot;;
}
NODE * convert(NODE *start)
{
    NODE *current = start;
    NODE *Left = NULL;
    NODE *Right = NULL;

    if(current == NULL)
    {
        return NULL;
    }

    Left = convert(current-&gt;left);
    Right = convert((current-&gt;right));

    current-&gt;right = Right;
    if (Left)
    {
        NODE *temp = Left;
        while(temp-&gt;right)
            temp = temp-&gt;right;

        temp-&gt;right = current;
        return Left;
    }
    else
        return current;
}

void print_BST(NODE *head)
{
    if(head == NULL)
        return;
    print_BST(head-&gt;left);
    cout&lt;&lt;head-&gt;data&lt;&lt;&quot; &quot;;
    print_BST(head-&gt;right);
}
void print_LL(NODE *start)
{
    while(start)
    {
        cout&lt;&lt;start-&gt;data&lt;&lt;&quot; &quot;;
        start = start-&gt;right;
    }
    cout&lt;&lt;endl;
}

void LevelOrderTraversal(NODE *root)
{
    queue&lt;NODE *&gt; Q;
    NODE *pop1 = NULL;

    if(root == NULL)
        return ;

    Q.push(root);

    while(!Q.empty())
    {
        pop1 = Q.front();
        Q.pop();

        if(pop1-&gt;newline == 0)
        {
            if(pop1-&gt;left)
                Q.push(pop1-&gt;left);
            if(pop1-&gt;right)
                Q.push(pop1-&gt;right);

            cout&lt;&lt;pop1-&gt;data&lt;&lt;&quot; &quot;;
        }
        else
            cout&lt;&lt;endl;

    }
    cout&lt;&lt;endl;
}
void printPath(int *path, int range)
{
    int i =0;
    while(i&lt;= range)
    {
        cout&lt;&lt;path[i++]&lt;&lt;&quot; &quot;;
    }
    cout&lt;&lt;endl;
}
static int path[100];
void hasSum (NODE *root, int sum, int index)
{
    static int originalSum = sum;

    if(root == NULL)
        return;

    if(root-&gt;data == sum)
    {
        path[index] = root-&gt;data;
        printPath(path, index);
        index = 0;
        return;
    }
    else if (root-&gt;data &lt; sum )
    {
        path[index] = root-&gt;data;
        hasSum(root-&gt;left, sum - root-&gt;data, index+1);
        hasSum(root-&gt;right, sum - root-&gt;data, index+1);
    }

    hasSum(root-&gt;left, originalSum, 0);
    hasSum(root-&gt;right, originalSum, 0);

    return;
}
int main() {

    NODE *head = NULL;
    NODE *start = NULL;
    int input[] = { 4, 2, 6, 1, 3, 5, 7};
    int size = sizeof(input)/sizeof(input[0]);

    head = form_BST(input, size);

    print_BST(head);

    cout&lt;&lt;endl;
    LevelOrderTraversal(head);
    hasSum(head, 6, 0);
    start = convert(head);
    print_LL(start);

    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=31&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2008/12/02/convert-bst-to-ll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
		<item>
		<title>Find the prime numbers up to 100</title>
		<link>http://gaanesh.wordpress.com/2008/10/28/find-the-prime-numbers-up-to-100/</link>
		<comments>http://gaanesh.wordpress.com/2008/10/28/find-the-prime-numbers-up-to-100/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 03:27:45 +0000</pubDate>
		<dc:creator>Ganesh</dc:creator>
				<category><![CDATA[Algorthim]]></category>

		<guid isPermaLink="false">http://gaanesh.wordpress.com/?p=14</guid>
		<description><![CDATA[Make a chart of the first one hundred positive integers (1-100): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=14&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Make a chart of the first one hundred positive integers (1-100):</p>
<pre>             1   2   3   4   5   6   7   8   9  10
            11  12  13  14  15  16  17  18  19  20
            21  22  23  24  25  26  27  28  29  30
            31  32  33  34  35  36  37  38  39  40
            41  42  43  44  45  46  47  48  49  50
            51  52  53  54  55  56  57  58  59  60
            61  62  63  64  65  66  67  68  69  70
            71  72  73  74  75  76  77  78  79  80
            81  82  83  84  85  86  87  88  89  90
            91  92  93  94  95  96  97  98  99 100</pre>
<ol>
<li>Cross out 1, because it is not prime.</li>
<li>Circle 2, because it is the smallest positive even prime. Now cross out every multiple of 2; in other words, cross out every second number.</li>
<li>Circle 3, the next prime. Then cross out all of the multiples of 3; in other words, every third number. Some, like 6, may have already been crossed out because they are multiples of 2.</li>
<li>Circle the next open number, 5. Now cross out all of the multiples of 5, or every 5th number.</li>
</ol>
<p>Continue doing this until all the numbers through 100 have either been circled or crossed out. You have just circled all the prime numbers from 1 to 100!</p>
<pre class="brush: cpp;">

#include &lt;iostream.h&gt;
#include &lt;bitset&gt;
using namespace std;
#define MAX 1000
void firstNPrime(int N)
{
    bitset&lt;MAX&gt; bitMap;
    bitMap.reset();

    cout&lt;&lt;&quot;Printing Prime Numbers&quot;&lt;&lt;endl;
    for(int i = 2; i&lt; N; )
    {
        for(int j=i+i; j&lt; N; j=j+i)
        {
            bitMap[j] = 1;
        }
        while(bitMap[++i]) ;
    }

    for(int k = 1; k&lt;N; k++)
    {
        if(!bitMap[k])
        {
            cout&lt;&lt;k&lt;&lt;&quot; &quot;;
        }
    }
    cout&lt;&lt;endl;

}
int main()
{
    cout&lt;&lt;&quot;Enter a no upto which u need the Prime Numbers: &quot;;
    int number;
    cin&gt;&gt;number;
    firstNPrime(number);
    return 0;
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gaanesh.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gaanesh.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gaanesh.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaanesh.wordpress.com&amp;blog=3114693&amp;post=14&amp;subd=gaanesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaanesh.wordpress.com/2008/10/28/find-the-prime-numbers-up-to-100/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ba06b795c852595893c8fa6b6ee4d14c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ganesh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
