<?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>one sorting algorithm &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/one-sorting-algorithm/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</title>
		<link>/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/</link>
		<comments>/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 07:13:01 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Negation]]></category>
		<category><![CDATA[Null]]></category>
		<category><![CDATA[one sorting algorithm]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[Scripting languages]]></category>

		<guid isPermaLink="false">/?p=1602</guid>
		<description><![CDATA[Here&#8217;s some Friday fun. Let me show you one sorting algorithm, perhaps the most known of all them &#8211; the quick sort, implemented both on PHP and JavaScript. Although the code look similar between both languages, there are few differences, that show the importance of the syntax knowledge! 1. PHP<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/" rel="bookmark" title="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="/2010/06/18/friday-algorithms-iterative-quicksort/" rel="bookmark" title="Friday Algorithms: Iterative Quicksort">Friday Algorithms: Iterative Quicksort </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/07/09/friday-algorithms-javascript-bubble-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Bubble Sort">Friday Algorithms: JavaScript Bubble Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s some Friday fun. Let me show you one sorting algorithm, perhaps the most known of all them &#8211; the quick sort, implemented both on PHP and JavaScript. Although the code look similar between both languages, there are few differences, that show the importance of the syntax knowledge!</p>
<h3>1. PHP</h3>
<pre lang="php">
<?php

    $unsorted = array(2,4,5,63,4,5,63,2,4,43);

    function quicksort($array)
    {
        if (count($array) == 0)
            return array();

        $pivot = $array[0];
        $left = $right = array();

        for ($i = 1; $i < count($array); $i++) {
            if ($array[$i] < $pivot)
                $left[] = $array[$i];
            else
                $right[] = $array[$i];
        }

        return array_merge(quicksort($left), array($pivot), quicksort($right));
    }

    $sorted = quicksort($unsorted);

    print_r($sorted);
</pre>
<h3>2. JavaScript</h3>
<pre lang="javascript">
var a = [2,4,5,63,4,5,63,2,4,43];

function quicksort(arr)
{
    if (arr.length == 0)
        return [];

    var left = new Array();
    var right = new Array();
    var pivot = arr[0];

    for (var i = 1; i < arr.length; i++) {
        if (arr[i] < pivot) {
           left.push(arr[i]);
        } else {
           right.push(arr[i]);
        }
    }

    return quicksort(left).concat(pivot, quicksort(right));
}

console.log(quicksort(a));
</pre>
<p>Note that the first conditional statement is quite important! While in PHP the count function will return 0 either on a NULL value or an empty array and you can substitute it with something like count($array) &lt; 2</p>
<pre lang="php">
if (count($array) < 2)
	return $array;
</pre>
<p>in JavaScript you cannot use that because of the presence of the 'undefined' value when an "empty" array is passed as an argument. Thus you've the conditional above:</p>
<pre lang="javascript">
// this will result with an error
if (arr.length < 2)
        return arr;
</pre>
<h2>Coming Up Next ...</h2>
<p>An iterative version of the algorithm next Friday!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/" rel="bookmark" title="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="/2010/06/18/friday-algorithms-iterative-quicksort/" rel="bookmark" title="Friday Algorithms: Iterative Quicksort">Friday Algorithms: Iterative Quicksort </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/07/09/friday-algorithms-javascript-bubble-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Bubble Sort">Friday Algorithms: JavaScript Bubble Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
