Over a million developers have joined DZone.
Platinum Partner

Quicksort Algorithm – Difference Between PHP and JavaScript

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

Here’s some Friday fun. Let me show you one sorting algorithm, perhaps the most known of all them – 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!



$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];
$right[] = $array[$i];

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

$sorted = quicksort($unsorted);



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) {
} else {

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


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) < 2

if (count($array) < 2)
return $array;

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:

// this will result with an error
if (arr.length < 2)
return arr;

Coming Up Next …

An iterative version of the algorithm next Friday!


The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


Published at DZone with permission of Stoimen Popov , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}