<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stoimen.com/blog &#187; javascript</title>
	<atom:link href="http://www.stoimen.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stoimen.com/blog</link>
	<description>web developing</description>
	<lastBuildDate>Wed, 28 Jul 2010 15:07:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Friday Algorithms: A Data Structure: JavaScript Stack</title>
		<link>http://www.stoimen.com/blog/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/</link>
		<comments>http://www.stoimen.com/blog/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 11:02:40 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Abstract data type]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Control flow]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Linked list]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Stack]]></category>
		<category><![CDATA[Subroutine]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Tree traversal]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1713</guid>
		<description><![CDATA[Algorithms and Data Structures Instead of writing about &#8220;pure&#8221; algorithms this time I&#8217;ve decided to write about data structures and to start with a stack implementation in JavaScript. However algorithms and data structures live together since the very beginning of the computer sciences. Another reason to write about data structures is that many algorithms need [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Iterative Quicksort'>Friday Algorithms: Iterative Quicksort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Algorithms and Data Structures</h2>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/stack.jpg"><img class="aligncenter size-full wp-image-1721" title="stack" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/stack.jpg" alt="stack" width="430" height="286" /></a></p>
<p>Instead of writing about &#8220;pure&#8221; algorithms this time I&#8217;ve decided to write about data structures and to start with a stack implementation in JavaScript. However algorithms and data structures live together since the very beginning of the computer sciences. Another reason to write about data structures is that many algorithms need a specific data structure to be implemented. Most of the search algorithms are data structure dependent. You know that searching into a tree is different from searching into a linked list.</p>
<p>I&#8217;d like to write more about searching in my future algorithm posts, but first we need some data structure examples. The first one is, as I mentioned &#8211; <a href="http://en.wikipedia.org/wiki/Stack_%28data_structure%29">stack</a>.</p>
<p>Implemented in JavaScript this is really a simple example, which don&#8217;t need much to be understood. But in first place what is a stack?</p>
<p>You can thing of the &#8220;computer science&#8221; stack as a stack of sheets of paper, as it&#8217;s shown on the picture. You&#8217;ve three basic operations. You can add new element to the stack by putting it on the top of the stack, so the previous top of the stack becomes the second element in the stack. Another operation is to &#8220;pop&#8221; from the stack &#8211; remove the &#8220;first&#8221; element in the stack &#8211; the top most element, and to return it. And finally print the stack. This is difficult to define as an operation, but let say you&#8217;ve to show somehow every element from the stack.</p>
<p>Here&#8217;s a little diagram:</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/07/stack.png"><img class="alignleft size-full wp-image-1837" title="stack" src="http://www.stoimen.com/blog/wp-content/uploads/2010/07/stack.png" alt="Stack" width="378" height="130" /></a></p>
<h2>Source Code</h2>
<p>At the end some source code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> data<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> next <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> stack <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">push</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> node<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span>.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> node<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            temp.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span>
            temp.<span style="color: #660066;">next</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span>.<span style="color: #660066;">next</span><span style="color: #339933;">;</span>
        temp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">print</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>node <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>node.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            node <span style="color: #339933;">=</span> node.<span style="color: #660066;">next</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> stack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
s.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
s.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> s.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
s.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Iterative Quicksort'>Friday Algorithms: Iterative Quicksort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Friday Algorithms: JavaScript Bubble Sort</title>
		<link>http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/</link>
		<comments>http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 10:20:06 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Bubble sort]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Insertion sort]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Merge sort]]></category>
		<category><![CDATA[Order theory]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[slowest algorithms]]></category>
		<category><![CDATA[Sorting]]></category>
		<category><![CDATA[Sorting algorithms]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1780</guid>
		<description><![CDATA[Bubble Sort This is one of the most slowest algorithms for sorting, but it&#8217;s extremely well known because of its easy to implement nature. However as I wrote past Fridays there are lots of sorting algorithms which are really fast, like the quicksort or mergesort. In the case of bubble sort the nature of the [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Merge Sort'>Friday Algorithms: JavaScript Merge Sort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Bubble Sort</h2>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/07/unsorted.jpg"><img class="alignleft size-full wp-image-1788" title="unsorted" src="http://www.stoimen.com/blog/wp-content/uploads/2010/07/unsorted.jpg" alt="Unsorted Array" width="250" height="333" /></a></p>
<p>This is one of the most slowest algorithms for sorting, but it&#8217;s extremely well known because of its easy to implement nature. However as I wrote past Fridays there are lots of sorting algorithms which are really fast, like the <a title="Iterative Quicksort" href="http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/" target="_blank">quicksort</a> or <a title="Merge Sort" href="http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/" target="_blank">mergesort</a>. In the case of bubble sort the nature of the algorithm is described in its name. The smaller element goes to the top (beginning) of the array as a bubble goes to the top of the water.</p>
<p>There is a cool animation showing how bubble sort works in compare to the quick sort and you can practically see how slow is bubble sort because of all the comparing.</p>
<p><a href="http://www.youtube.com/watch?v=vxENKlcs2Tw">QuickSort vs. BubbleSort</a></p>
<h2>Pseudo Code</h2>
<p>Actually what I&#8217;d like to show you is how you can move from pseudo code to code in practice. Here&#8217;s the pseudo code from <a href="http://en.wikipedia.org/wiki/Bubble_sort" title="BubbleSort Wikipedia" target="_blank">Wikipedia</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">procedure bubbleSort<span style="color: #009900;">&#40;</span> A <span style="color: #339933;">:</span> list of sortable items <span style="color: #009900;">&#41;</span> defined <span style="color: #000066; font-weight: bold;">as</span><span style="color: #339933;">:</span>
  <span style="color: #000066; font-weight: bold;">do</span>
    swapped <span style="color: #339933;">:=</span> <span style="color: #003366; font-weight: bold;">false</span>
    <span style="color: #000066; font-weight: bold;">for</span> each i <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #CC0000;">0</span> to length<span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">2</span> inclusive <span style="color: #000066; font-weight: bold;">do</span><span style="color: #339933;">:</span>
      <span style="color: #000066; font-weight: bold;">if</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> then
        swap<span style="color: #009900;">&#40;</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
        swapped <span style="color: #339933;">:=</span> <span style="color: #003366; font-weight: bold;">true</span>
      end <span style="color: #000066; font-weight: bold;">if</span>
    end <span style="color: #000066; font-weight: bold;">for</span>
  <span style="color: #000066; font-weight: bold;">while</span> swapped
end procedure</pre></div></div>

<h2>JavaScript Source</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> bubbleSort<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> swapped<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>
        swapped <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> a.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                a<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span>
                swapped <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>swapped<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
bubbleSort<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As a result you&#8217;ve a sorted array!</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/07/sorted.jpg"><img class="aligncenter size-full wp-image-1790" title="sorted" src="http://www.stoimen.com/blog/wp-content/uploads/2010/07/sorted.jpg" alt="Sorted Array" width="430" height="353" /></a></p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Merge Sort'>Friday Algorithms: JavaScript Merge Sort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Friday Algorithms: JavaScript Merge Sort</title>
		<link>http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/</link>
		<comments>http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 10:28:16 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Merge sort]]></category>
		<category><![CDATA[Sorting algorithms]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1746</guid>
		<description><![CDATA[Merge Sort This week I&#8217;m going to cover one very popular sorting algorithm &#8211; the merge sort. It&#8217;s very intuitive and simple as it&#8217;s described in Wikipedia: If the list is of length 0 or 1, then it is already sorted. Otherwise: Divide the unsorted list into two sublists of about half the size. Sort [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Merge Sort</h2>
<p>This week I&#8217;m going to cover one very popular sorting algorithm &#8211; the merge sort. It&#8217;s very intuitive and simple as it&#8217;s described in <a title="Merge Sort" href="http://en.wikipedia.org/wiki/Merge_sort" target="_blank">Wikipedia</a>:</p>
<ul>
<li>If the list is of length 0 or 1, then it is already sorted.  Otherwise:</li>
<li>Divide the unsorted list into two sublists of about half the size.</li>
<li>Sort each sublist recursively by re-applying merge sort.</li>
<li>Merge the two sublists back into one sorted list.</li>
</ul>
<h2>Here&#8217;s the Source</h2>
<p><em>(JavaScript)</em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> mergeSort<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arr.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">return</span> arr<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> middle <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>arr.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> left   <span style="color: #339933;">=</span> arr.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> middle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> right  <span style="color: #339933;">=</span> arr.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span>middle<span style="color: #339933;">,</span> arr.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> merge<span style="color: #009900;">&#40;</span>mergeSort<span style="color: #009900;">&#40;</span>left<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> mergeSort<span style="color: #009900;">&#40;</span>right<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> merge<span style="color: #009900;">&#40;</span>left<span style="color: #339933;">,</span> right<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>left.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;&amp;</span> right.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>left<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> right<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            result.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>left.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            result.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>right.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>left.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
        result.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>left.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>right.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
        result.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>right.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>mergeSort<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s interesting to see what happens in the Firebug&#8217;s console:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">203</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">746</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">984</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">198</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">764</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>Actually the tricky part in this algorithm is the merge function &#8211; it does all the work. </p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replace the Broken Images with a Default Image with JavaScript</title>
		<link>http://www.stoimen.com/blog/2010/07/01/replace-the-broken-images-with-a-default-image-with-javascript/</link>
		<comments>http://www.stoimen.com/blog/2010/07/01/replace-the-broken-images-with-a-default-image-with-javascript/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 12:35:33 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1738</guid>
		<description><![CDATA[There is cool JavaScript trick that helps you catch broken images. You know that when the image doesn&#8217;t exist, the http path to the image returns 404 or the path is wrong, the browser doesn&#8217;t display nothing in the most cases. As MSIE is always different it displays an ugly icon saying that there is [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2009/08/05/jquery-check-whether-image-is-loaded/' rel='bookmark' title='Permanent Link: jQuery: check whether image is loaded'>jQuery: check whether image is loaded</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/' rel='bookmark' title='Permanent Link: Speed up the JavaScript. It can change dramatically the user experience.'>Speed up the JavaScript. It can change dramatically the user experience.</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/11/javascript-optimization-lazy-loading/' rel='bookmark' title='Permanent Link: JavaScript optimization. Lazy loading.'>JavaScript optimization. Lazy loading.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There is cool JavaScript trick that helps you catch broken images. You know that when the image doesn&#8217;t exist, the http path to the image returns 404 or the path is wrong, the browser doesn&#8217;t display nothing in the most cases. As MSIE is always different it displays an ugly icon saying that there is not an image to load</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/07/msie-broken-image.png"><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/07/msie-broken-image.png" alt="MSIE broken image icon" title="msie-broken-image" width="148" height="224" class="aligncenter size-full wp-image-1747" /></a></p>
<p>and that is really bad!</p>
<h2>There&#8217;s a Quick Fix &#8230;</h2>
<p>Simply add an onerror handler on the IMG tag</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://..../broken_url.jpg&quot;</span> onerror<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.src='path_to_default_image'&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2009/08/05/jquery-check-whether-image-is-loaded/' rel='bookmark' title='Permanent Link: jQuery: check whether image is loaded'>jQuery: check whether image is loaded</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/' rel='bookmark' title='Permanent Link: Speed up the JavaScript. It can change dramatically the user experience.'>Speed up the JavaScript. It can change dramatically the user experience.</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/11/javascript-optimization-lazy-loading/' rel='bookmark' title='Permanent Link: JavaScript optimization. Lazy loading.'>JavaScript optimization. Lazy loading.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/07/01/replace-the-broken-images-with-a-default-image-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 geolocation &#8211; What If the User Doesn&#8217;t Share His Position?</title>
		<link>http://www.stoimen.com/blog/2010/06/29/html5-geolocation-what-if-the-user-doesnt-share-his-position/</link>
		<comments>http://www.stoimen.com/blog/2010/06/29/html5-geolocation-what-if-the-user-doesnt-share-his-position/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 18:29:01 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Free software]]></category>
		<category><![CDATA[FTP clients]]></category>
		<category><![CDATA[Geo]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[mobile phones]]></category>
		<category><![CDATA[Mozilla Firefox]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1720</guid>
		<description><![CDATA[HTML5 Geolocation So far we were used to expect something like this from our mobile phones with built-in GPS support. Every image or video clip then was automatically &#8220;tagged&#8221; with latitude &#38; longitude geo data. With HTML5 coming features we discover new cool things we can do with our browsers. Such cool thing is the [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/' rel='bookmark' title='Permanent Link: Does Firefox play mp4 h.264 within the HTML5 video tag?'>Does Firefox play mp4 h.264 within the HTML5 video tag?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/24/storing-javascript-objects-in-html5-localstorage/' rel='bookmark' title='Permanent Link: Storing JavaScript objects in html5 localStorage'>Storing JavaScript objects in html5 localStorage</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/26/html5-video-support-detecting-playing-and-progressive-enhancement/' rel='bookmark' title='Permanent Link: HTML5 video support. Detecting, playing and progressive enhancement!'>HTML5 video support. Detecting, playing and progressive enhancement!</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>HTML5 Geolocation</h2>
<p>So far we were used to expect something like this from our mobile phones with built-in GPS support. Every image or video clip then was automatically &#8220;tagged&#8221; with latitude &amp; longitude geo data. With HTML5 coming features we discover new cool things we can do with our browsers. Such cool thing is the geolocation.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/geo-location.jpg"><img class="aligncenter size-full wp-image-1726" title="geo-location" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/geo-location.jpg" alt="Geo Location" width="430" height="134" /></a></p>
<h2>Support</h2>
<p>As you may guess not every browser is supporting these HTML5 features, but out in the web there is quite good collection of tables comparing different browsers and their support level.</p>
<h2>Firefox</h2>
<p>This &#8211; as expected is a browser that supports this geo tagging. First of all you&#8217;ve to allow your browser to use your geo coordinates, as this can be private information.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/browser-geo-location.png"><img class="aligncenter size-full wp-image-1728" title="browser-geo-location" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/browser-geo-location.png" alt="Browser GEO Location" width="430" height="51" /></a></p>
<p>Once you do it you can access the geo coordinates, which by the way are quite accurate, with JavaScript.</p>
<h2>What if you don&#8217;t share your position?</h2>
<p>What happens if you don&#8217;t want to share your position? Actually I ran in this situation and as my application waited the coordinates &#8211; it was completely blocked.</p>
<p>The examples doesn&#8217;t show you something special. They simply describe how to get the coordinates, but doesn&#8217;t tell you what if the user doesn&#8217;t click on the &#8220;share&#8221; button.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!!</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>getPos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
...
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getPos<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
    position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course getPos() can be simply an anonymous function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!!</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
            position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Only the Firefox documentation tells you how to handle errors, simply add one more parameter &#8211; callback, for getCurrentPosition() method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!!</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>getPos<span style="color: #339933;">,</span> <span style="color: #000066;">onError</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
...
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getPos<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
    position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">onError</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// handle error</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you know where you don&#8217;t want to be.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/map-marker.jpeg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/map-marker.jpeg" alt="Map Marker" title="map-marker" width="430" height="286" class="aligncenter size-full wp-image-1731" /></a></p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/' rel='bookmark' title='Permanent Link: Does Firefox play mp4 h.264 within the HTML5 video tag?'>Does Firefox play mp4 h.264 within the HTML5 video tag?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/24/storing-javascript-objects-in-html5-localstorage/' rel='bookmark' title='Permanent Link: Storing JavaScript objects in html5 localStorage'>Storing JavaScript objects in html5 localStorage</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/26/html5-video-support-detecting-playing-and-progressive-enhancement/' rel='bookmark' title='Permanent Link: HTML5 video support. Detecting, playing and progressive enhancement!'>HTML5 video support. Detecting, playing and progressive enhancement!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/29/html5-geolocation-what-if-the-user-doesnt-share-his-position/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</title>
		<link>http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/</link>
		<comments>http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 09:30:35 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[quicksort algorithm]]></category>
		<category><![CDATA[Selection algorithm]]></category>
		<category><![CDATA[Sort]]></category>
		<category><![CDATA[sorting algorithm]]></category>
		<category><![CDATA[Sorting algorithms]]></category>
		<category><![CDATA[Spreadsort]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1689</guid>
		<description><![CDATA[Yes! It&#8217;s really really fast, and it&#8217;s far quicker than the quicksort algorithm, which is considered as the fastest sorting algorithm in practice. However how it&#8217;s possible to be faster than the quicksort, which is the fastest algorithm?! Is that true? Actually it&#8217;s true, but only in few cases. It works with integers, you&#8217;ve to [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Iterative Quicksort'>Friday Algorithms: Iterative Quicksort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Yes! It&#8217;s really really fast, and it&#8217;s far quicker than the <a href="http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/">quicksort</a> algorithm, which is considered as the fastest sorting algorithm in practice. However how it&#8217;s possible to be faster than the quicksort, which is the fastest algorithm?! Is that true? Actually it&#8217;s true, but only in few cases. It works with integers, you&#8217;ve to know the first and the last element from that set and you&#8217;ve to be sure that every element is unique</p>
<p>Imagine you&#8217;ve a set of numbers all of them greater than 1 and lesser than 1000. Of course you&#8217;re not suppose to have all of the integers between 1 and 1000, but only few of them &#8211; think of 500 numbers between 1 and 1000! Here&#8217;s important to note &#8211; that this is only an example, you can have far more than only few numbers between 1 and 1000 &#8211; what about the numbers between 1 and 1,000,000 &#8211; this is a big set, isn&#8217;t it.</p>
<p>The question is &#8211; if there are so many constraints, why should I use that algorithm instead of quicksort, or another sorting algorithm, that works with everything. The answer is clear &#8211; yes, you&#8217;d prefer quicksort if you&#8217;ve to sort some arbitrary data, but when it comes to integers, and you&#8217;ve, let&#8217;s say, 1,000,000 integers, my advice is &#8211; use this algorithm!</p>
<h2>Sorting the Set</h2>
<h3>1. First Pass</h3>
<p>First we have an unsorted array, but we know the minimum and maximum of the set.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/unsorted.png"><img class="aligncenter size-full wp-image-1701" title="unsorted" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/unsorted.png" alt="an unsorted array of integers" width="283" height="111" /></a></p>
<p>On the first pass initialize an empty array with as many elements, as they are between the first and the last element of the set &#8211; for a set between 1 and 1000 &#8211; that will be an array with 1000 elements &#8211; each of which will be a zero in the beginning.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/temp-array.png"><img class="aligncenter size-full wp-image-1700" title="temp-array" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/temp-array.png" alt="temporary array with 0 on every element" width="277" height="138" /></a></p>
<p>Than loop trough the set and for every element in the set &#8211; you should put a 1 on it&#8217;s place</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/array-first-pass.png"><img class="aligncenter size-full wp-image-1699" title="array-first-pass" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/array-first-pass.png" alt="sort a set - first pass" width="299" height="162" /></a></p>
<p>Now we have an array of 0 and 1.</p>
<h3>2. Second Pass</h3>
<p>After the first pass, you&#8217;d guess what you&#8217;ve to do &#8211; loop trough the second array and print the keys of the elements different from 0 &#8211; those that are 1.</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/sorted.png"><img class="aligncenter size-full wp-image-1698" title="sorted" src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/sorted.png" alt="a sorted set of integers" width="312" height="231" /></a></p>
<p>Now the array is sorted!</p>
<h2>Source Code</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">34</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">203</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">746</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">984</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">764</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setSort<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> arr.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        t<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        t<span style="color: #009900;">&#91;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">==</span> t<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
setSort<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now that you&#8217;ve seen that algorithm, perhaps you&#8217;d guess that it&#8217;s no so difficult to change from integers to any other set, and once again I should say that in many cases this is the best algorithm for sorting! Very often quicksort is preferred, but not always there isn&#8217;t something faster!</p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Iterative Quicksort'>Friday Algorithms: Iterative Quicksort</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Inject JavaScript Code in a Action/View</title>
		<link>http://www.stoimen.com/blog/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/</link>
		<comments>http://www.stoimen.com/blog/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 14:39:49 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Client-side JavaScript]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Printf]]></category>
		<category><![CDATA[Scripting languages]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1666</guid>
		<description><![CDATA[There is a view helper that can inject a head or inline script into your code. Simply by putting some JavaScript code into the view script wont do the job, because the .phtml file is grabbed and parsed by the Zend Framework and thus the code there wont be parsed as JavaScript and it wont [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/23/bind-zend-action-with-non-default-view-part-2/' rel='bookmark' title='Permanent Link: Bind Zend Action with Non-Default View &#8211; Part 2'>Bind Zend Action with Non-Default View &#8211; Part 2</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/10/manage-javascript-and-css-includes-within-zend-framework-application/' rel='bookmark' title='Permanent Link: Manage JavaScript and CSS includes within Zend Framework application'>Manage JavaScript and CSS includes within Zend Framework application</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/07/bind-zend-action-with-non-default-view/' rel='bookmark' title='Permanent Link: Bind Zend Action with Non-Default View'>Bind Zend Action with Non-Default View</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There is a view helper that can inject a head or inline script into your code.</p>
<p>Simply by putting some JavaScript code into the view script wont do the job, because the .phtml file is grabbed and parsed by the Zend Framework and thus the code there wont be parsed as JavaScript and it wont be executed as well by the browser.</p>
<h2>The Most Simple Way &#8230;</h2>
<p>is to put some place holder into the layout .phtml file and than from the controller&#8217;s action you can assign some JS code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">// layout.phtml
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #339933;">...</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">scriptTags</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">scriptTags</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;script src=&quot;my.js&quot;&gt;&lt;/script&gt;'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Solution No. 2</h2>
<p>Although the first solution is correct it&#8217;s not the most clearest way, because you put the JavaScript code into the PHP, and this sooner or later becomes a mess.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">scriptTags</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;script src=&quot;my.js&quot;&gt;&lt;/script&gt;'</span><span style="color: #339933;">;</span>
						  <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;script&gt;alert(&quot;here\'s my&quot; + &quot;test&quot;)&lt;/script&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The IDE is not highlighting the code, and you&#8217;ve to deal with too many quotes, which is bad! There is a better solution however &#8211; the inline script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// script tags</span>
<span style="color: #666666; font-style: italic;">/* @var $scripts Zend_View_Helper_InlineScript */</span>
<span style="color: #000088;">$scripts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inlineScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$scripts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Simply the Best</h2>
<p>Yeah, the best solution is something even better than the second one. Although the solution I&#8217;ve just mentioned is fine for including JS files, when you&#8217;ve to add some JS code you&#8217;ll have to put it again into the PHP.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'some dynamically generated message'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// script tags</span>
<span style="color: #666666; font-style: italic;">/* @var $scripts Zend_View_Helper_InlineScript */</span>
<span style="color: #000088;">$scripts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inlineScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$scripts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$scripts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendScript</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alert(&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$msg</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If there&#8217;s no relation between JavaScript and PHP you can simply put all this code into a separate .js file and load it from there. This is by the way the best solution because the file is cached by the browser, if the cache is enabled. But most of the time you perhaps have to send some variables from PHP to the JavaScript code.</p>
<p>It would be perfect if you had some kind of a variable placeholders &#8211; exactly as you have it in the Zend Framework&#8217;s view scripts!</p>
<h3>Another View</h3>
<p>This is completely possible &#8211; just forget about the default view &#8211; it renders the view script and it&#8217;s bind to the /views/scripts folder. You can make another, completely new, view instead! Here&#8217;s some source:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBasePath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path_to_the_js_folder'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Some dynamically generated message'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// script tags</span>
<span style="color: #666666; font-style: italic;">/* @var $scripts Zend_View_Helper_InlineScript */</span>
<span style="color: #000088;">$scripts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inlineScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$scripts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$scripts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendScript</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'inline.js'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus now in the JS file you can have some placeholders!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// inline.js</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// some js code</span>
<span style="color: #006600; font-style: italic;">// ...</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;?php echo $this-&gt;msg ?&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/23/bind-zend-action-with-non-default-view-part-2/' rel='bookmark' title='Permanent Link: Bind Zend Action with Non-Default View &#8211; Part 2'>Bind Zend Action with Non-Default View &#8211; Part 2</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/10/manage-javascript-and-css-includes-within-zend-framework-application/' rel='bookmark' title='Permanent Link: Manage JavaScript and CSS includes within Zend Framework application'>Manage JavaScript and CSS includes within Zend Framework application</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/07/bind-zend-action-with-non-default-view/' rel='bookmark' title='Permanent Link: Bind Zend Action with Non-Default View'>Bind Zend Action with Non-Default View</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery: Get the Selected Option Text</title>
		<link>http://www.stoimen.com/blog/2010/06/22/jquery-get-the-selected-option-text/</link>
		<comments>http://www.stoimen.com/blog/2010/06/22/jquery-get-the-selected-option-text/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 17:33:56 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[Technical communication]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1660</guid>
		<description><![CDATA[What&#8217;s the Task? First of all let me explain what is the task. If there&#8217;s a select menu with several options, each of which are with a given value attribute, the task is to get the text into the option tag, and not that of the value attribute. In that scenario let&#8217;s assume that we [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/19/speed-up-the-jquery-code-selectors-cache/' rel='bookmark' title='Permanent Link: Speed Up the jQuery Code: Selectors&#8217; Cache'>Speed Up the jQuery Code: Selectors&#8217; Cache</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/16/jquery-tooltip-plugin/' rel='bookmark' title='Permanent Link: jQuery tooltip plugin!'>jQuery tooltip plugin!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/17/build-a-list-tree-with-the-beauty-of-jquery/' rel='bookmark' title='Permanent Link: Build a List Tree with the Beauty of jQuery'>Build a List Tree with the Beauty of jQuery</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>What&#8217;s the Task?</h2>
<p>First of all let me explain what is the task. If there&#8217;s a select menu with several options, each of which are with a given value attribute, the task is to get the text into the option tag, and not that of the value attribute.</p>
<p>In that scenario let&#8217;s assume that we have the following HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-select&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>value 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>value 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/Picture-1.png"><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/Picture-1.png" alt="select menu" title="Select Menu" width="181" height="95" class="alignleft size-full wp-image-1671" /></a><br />
Note that in the official jQuery doc page, the selector is described in the same context, but with a different markup:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-select&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span>&gt;</span>value 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span>&gt;</span>value 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p>than the jQuery snippet looks like that:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#my-select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s interesting to note that there&#8217;s no value attribute set to the options, thus the selection of an element is correct and the returned value is correct. The problem is that this code doesn&#8217;t work correctly once you&#8217;ve a different value attribute, from the value enclosed into the option tag. In our case we&#8217;ve the markup shown on the first code snippet and we&#8217;d like to get the &#8220;value 2&#8243; string instead of &#8220;2&#8243;.<br />
<a href="http://www.stoimen.com/blog/wp-content/uploads/2010/06/Picture-2.png"><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/Picture-2.png" alt="" title="Alert screen" width="329" height="130" class="alignleft size-full wp-image-1672" /></a></p>
<h2>Source Code</h2>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-select&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>value 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>value 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#my-select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Solution</h2>
<p>Finally the solution, is pretty simple and clear, but this time is not to so &#8220;native&#8221; and it&#8217;s definitely something you couldn&#8217;t expect! The only thing you&#8217;ve to do is to change the selector!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#my-select).change(function() {
        alert($('#my-select option:selected').html());
    });
});</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/19/speed-up-the-jquery-code-selectors-cache/' rel='bookmark' title='Permanent Link: Speed Up the jQuery Code: Selectors&#8217; Cache'>Speed Up the jQuery Code: Selectors&#8217; Cache</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/16/jquery-tooltip-plugin/' rel='bookmark' title='Permanent Link: jQuery tooltip plugin!'>jQuery tooltip plugin!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/17/build-a-list-tree-with-the-beauty-of-jquery/' rel='bookmark' title='Permanent Link: Build a List Tree with the Beauty of jQuery'>Build a List Tree with the Beauty of jQuery</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/22/jquery-get-the-selected-option-text/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Speed Up the jQuery Code: Selectors&#8217; Cache</title>
		<link>http://www.stoimen.com/blog/2010/06/19/speed-up-the-jquery-code-selectors-cache/</link>
		<comments>http://www.stoimen.com/blog/2010/06/19/speed-up-the-jquery-code-selectors-cache/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 08:36:08 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[measurement]]></category>
		<category><![CDATA[results]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1630</guid>
		<description><![CDATA[Short Experiment Here&#8217;s a short optimization of a small chunk of jQuery code. The experiment was to increment the number of DOM element access. In this case this was a change of the innerHTML property &#8211; using the $.html() method. I&#8217;ve measured the result with the console.time() method and thus I expect correct results for [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/02/02/firebugs-console-time-accuracy/' rel='bookmark' title='Permanent Link: Firebug&#8217;s console.time() accuracy'>Firebug&#8217;s console.time() accuracy</a></li>
<li><a href='http://www.stoimen.com/blog/2009/11/10/jquery-css-functions-part-1-offset/' rel='bookmark' title='Permanent Link: jQuery CSS functions. Part 1 &#8211; offset()'>jQuery CSS functions. Part 1 &#8211; offset()</a></li>
<li><a href='http://www.stoimen.com/blog/2009/11/04/jquery-css-selectors-change-one-or-more-css-properties/' rel='bookmark' title='Permanent Link: jQuery CSS selectors. Change one or more CSS properties!'>jQuery CSS selectors. Change one or more CSS properties!</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Short Experiment</h2>
<p>Here&#8217;s a short optimization of a small chunk of jQuery code. The experiment was to increment the number of DOM element access. In this case this was a change of the innerHTML property &#8211; using the $.html() method.</p>
<p>I&#8217;ve measured the result with the console.time() method and thus I expect correct results for both cases. In the first case I directly call the jQuery selectors&#8217; html() method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While in the second case I &#8220;cache&#8221; it before the loop:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    t.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and here&#8217;s the full code in the first case:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>jQuery Cache<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/scripts/library.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
$(document).ready(function() {
&nbsp;
    console.time('foo');
    for (var i = 0; i <span style="color: #009900;">&lt; <span style="color: #cc66cc;">10000</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">        $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#container'</span><span style="color: #66cc66;">&#41;</span>.html<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">    <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">    console.timeEnd<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<h2>The Results</h2>
<p>As expected the second &#8220;cached&#8221; approach gave better results. With the increment of the iterations the second method began to be slightly better than the first one. That&#8217;s not so much, but imagine you have more than one selector in the loop&#8217;s body?</p>
<h3>Numbers:</h3>
<p><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/table.png" /><br />
Note that in the time is in ms, and, yes, this is not so much, but when you deal with large data and you&#8217;ve more than one selector in the loop&#8217;s body, this can be critical!</p>
<h3>Graphics:</h3>
<p><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/chart.png" /></p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/02/02/firebugs-console-time-accuracy/' rel='bookmark' title='Permanent Link: Firebug&#8217;s console.time() accuracy'>Firebug&#8217;s console.time() accuracy</a></li>
<li><a href='http://www.stoimen.com/blog/2009/11/10/jquery-css-functions-part-1-offset/' rel='bookmark' title='Permanent Link: jQuery CSS functions. Part 1 &#8211; offset()'>jQuery CSS functions. Part 1 &#8211; offset()</a></li>
<li><a href='http://www.stoimen.com/blog/2009/11/04/jquery-css-selectors-change-one-or-more-css-properties/' rel='bookmark' title='Permanent Link: jQuery CSS selectors. Change one or more CSS properties!'>jQuery CSS selectors. Change one or more CSS properties!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/19/speed-up-the-jquery-code-selectors-cache/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Friday Algorithms: Iterative Quicksort</title>
		<link>http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/</link>
		<comments>http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 08:21:27 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[c.a.r. hoare]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[interative]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=1631</guid>
		<description><![CDATA[Sorting Algorithms Last Friday I wrote a quick post showing the recursive version of quicksort on both PHP and JavaScript, but this time let me begin with some breve history. Actually sorting and searching algorithms are widely used in the computer science and are developed and improved many times in the last 60 years or [...]


Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Sorting Algorithms</h2>
<p>Last Friday I wrote a quick <a href="http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/">post</a> showing the recursive version of quicksort on both PHP and JavaScript, but this time let me begin with some breve history. Actually sorting and searching algorithms are widely used in the computer science and are developed and improved many times in the last 60 years or so. As the world goes so wired, all the information around is stored in data bases and the need of fast search methods is critical. But as we all know from our experience, it is far more easy to search into a sorted data structure. Such are all the dictionaries we use. Can you, actually, imagine to search for a word in a dictionary where all the words don&#8217;t follow the alphabetical order and are dispersed chaotically instead?</p>
<p>That&#8217;s why from the early years of computer science both sorting and searching algorithms go together. I&#8217;ll cover more on search algorithms in the future, but as I started with quicksort &#8211; one of the many sorting algorithms, let me mention that this algorithm is not anonymous! What I mean is that his author is well known and he&#8217;s recognized in the computer science community. His name is &#8230;</p>
<h2>C. A. R. Hoare</h2>
<p><img src="http://www.stoimen.com/blog/wp-content/uploads/2010/06/Hoare.jpg" alt="Sir C. A. R. Hoare" /><br />
Sir Charles Antony Richard Hoare is born on the 11 of January 1934 in Ceylon, now Sri Lanka, and after finishing his studies in the University of Oxford and being in the Royal Navy for two years, he went to &#8230; Moscow where at the age of 26 (in 1960) he developed the famous quicksort algorithm. That&#8217;s not everything! Beside the sorting algorithm he&#8217;s well recognized with the Hoare logic and the invention of the formal language CSP (Communicating Sequential Processes).</p>
<p>Here&#8217;s what Sir C. A. R. Hoare wrote in his lecture &#8220;The Emperor’s Old Clothes&#8221; published by Communications of the ACM in 1981:</p>
<blockquote><p>My first task was to implement for the new Elliot 803 computer, a  library subroutine for a new fast method of internal sorting just  invented by Shell. I greatly enjoyed the challenge of maximizing  efficiency in the simple decimal-addressed machine code of those days.  My boss and tutor, Pat Shackleton, was very pleased with my completed  program. I then said timidly that I thought I had invented a sorting  method that would usually run faster than SHELLSORT, without taking much  extra store. He bet me sixpence that I had not. Although my method was  very difficult to explain, he finally agreed that I had won my bet.</p></blockquote>
<p>The quicksort was born!</p>
<h2>Recursive vs. Iterative</h2>
<p>There are two main directions in solving such problems as the sorting algorithms. Recursive way and the iterative way. Every developer knows what is recursive and what&#8217;s iterative. We like to say that recursive is more &#8220;elegant&#8221; solution, than the iterative, but it&#8217;s interesting to say that both are completely interchangeable. Yeah, every recursion can be modeled with a stack (as the recursion is always converted into a stack on the machine level), and every loop can be converted to a recursion. It&#8217;s interesting to say that the second direction is easier, while the switch from recursion to stack it&#8217;s not always obvious and easy!</p>
<p>Here&#8217;s a code snippet from the recursive version of the quicksort, I&#8217;ve <a href="http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/">posted</a> last Friday:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">return</span> quicksort<span style="color: #009900;">&#40;</span>left<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span>pivot<span style="color: #339933;">,</span> quicksort<span style="color: #009900;">&#40;</span>right<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What actually happens here is that the second call of quicksort() is always delayed for a later execution. Until the first quicksort() doesn&#8217;t finish his job, the second is never used! Why than we need a recursion? Why don&#8217;t we put the right part of the array in a stack for later execution? In fact this is the solution! Every right part will be put into a stack. Thus while the stack has some elements we pop an element from it and than split it again on two &#8211; left and right: the right part enters the stack and the left is yet again sorted.</p>
<p>In that scenario we end with an element split when the left part is with only one element. Here&#8217;s important to say that this might not be the perfect solution and there might be some quite good optimizations!</p>
<p>Assuming we&#8217;ve the following array &#8211; [8,4,6,2,1,9,5], here&#8217;s what happens, note that in JavaScript array.pop() pops the rightmost element, in this case 5:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
1. <span style="color: #660066;">step</span>
stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
2. <span style="color: #660066;">step</span>
stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
3. <span style="color: #660066;">step</span>
stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
4. <span style="color: #660066;">step</span>
stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span>
&nbsp;
5. <span style="color: #660066;">step</span>
stack <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span>
sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span>
...</pre></div></div>

<h2>Code</h2>
<p>Here&#8217;s the implementation of the iterative version, again on both PHP and JavaScript:</p>
<p><strong>JavaScript:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> qsort<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>arr<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> sorted <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>stack.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> stack.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> tl <span style="color: #339933;">=</span> temp.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tl <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sorted.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> pivot <span style="color: #339933;">=</span> temp<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> left <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> right <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> tl<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> pivot<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                left.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                right.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        left.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>pivot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>right.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
            stack.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>right<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>left.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
            stack.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>left<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>sorted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
qsort<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>PHP:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$unsorted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> qsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$stack</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sorted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stack</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$sorted</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$pivot</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$left</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$right</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pivot</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$left</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$right</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$left</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pivot</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$right</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$right</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$left</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$left</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sorted</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$sorted</span> <span style="color: #339933;">=</span> qsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First of all this is more code than the recursive solution, and besides that it&#8217;s perhaps not the optimal solution &#8211; I&#8217;ll cover in the future more on how to optimize it and where it can be useful.</p>
<p>Till next Friday!</p>


<p>Related posts:<ol><li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Permanent Link: Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/' rel='bookmark' title='Permanent Link: Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!'>Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/' rel='bookmark' title='Permanent Link: Friday Algorithms: JavaScript Bubble Sort'>Friday Algorithms: JavaScript Bubble Sort</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2010/06/18/friday-algorithms-iterative-quicksort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
