Friday Algorithms: Quicksort – Difference Between PHP and JavaScript

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!

1. 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];
                $right[] = $array[$i];
        return array_merge(quicksort($left), array($pivot), quicksort($right));
    $sorted = quicksort($unsorted);

2. 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) {
        } 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!

6 thoughts on “Friday Algorithms: Quicksort – Difference Between PHP and JavaScript

  1. Hi, I have to make javascript quick sort animation but I have some troubles. Please… don’t you know, how to make it possible? Because of undefined statement I’m quite disoriented.

  2. (val[] is array)

    I need quick sort that doesn’t work with buffers, like you did. Just let it work on one array, because I want to animate it and each swap save in another array to use it on animating etc… This algorithm works with PHP, C++, but with this implementation, it doesn’t work. In while loops I get into undefined statement and whole algorithm loops forever.

  3. I did mistake. =) I meant, that this implementation works with PHP, C++, Pascal but with javascript it doesn’t work, because of undefined statement. There are lots of implementation, but no one is suited for animation that I need to school project.

    Thank you very much! =)

  4. I started to study algorithm (and started with sorting algorithms of course!) and found some interesting thing. I looked on the web for examples and all use at iteration something like that (and you too):
    for ($i = 1; $i < count($array); $i++)…
    I found that is not efficient as time consuption because array elements must be counted at every iteration. I think is optim to memorize counted value first. I run some test and found that.
    What is your opinion?

  5. Hi tzulac,

    Indeed that is a bad practice. However I made it because it’s more important to show the frame of the algorithm. As you know different environemnts have different approaches when trying to optimize a chunk of code and mine is PHP. Thus you can go for a different solution in other languages. That is why I just didn’t focus on this part of the code.

    However, thanks for the remark!

Leave a Reply

Your email address will not be published. Required fields are marked *