Recursive bubble sort algorithm in PHP

Recursive bubble sort algorithm in PHP

Created:06 Apr 2017 17:48:19 , in  Maths

In this article I'm going to take a quick look at arranging elements of a list in a chosen order, or simply speaking - sorting, with a twist, though. Many algorithms have been devised for the purpose of sorting over the last sixty or so years. Arguably, one of the oldest and more popular of them is bubble sort.

Bubble sort is a stable but also inefficient algorithm when it comes to sorting large list. But hey, rather than for blazingly fast execution I'm looking to play with some PHP features here now.

Moreover, bubble sort is a fairly simple algorithm. It can be implemented really quick using recursion (a twist;) and anonymous functions ( it's a pity they can't be passed as optional arguments to a function in PHP, though - another, negative twist this time ).

Getting to nitty-gritty

My recursive implementation of bubble sort algorithm is a static class method. Apart from this it differs little from PHP sort function, in particular in terms of how it sorts.

As for arguments, it accepts three of them ( the third one is not really usable, though - yet another twist ;).

First argument is a list of items to sort, second - optional function to use for sorting items in the list. Nothing new here by now, really.

Third argument, called p, keeps track of number of passes made on the list (see article on bubble sort on for more on bubble sort passes ). I used argument instead of a static class variable to implement recursion in this case. P is set on the basis of length of the first argument automatically and is not supposed to be changed at any time by the user (Just another little twist here ;)).

/* Class: RecursiveSort - recursive sort algorithms container
   Author : Sylwester Wojnowski
   WWW :
     public static bubble($list, $compare)
         sort items in $list  
         $list array - items (integers, strings, arrays, objects)
         $compare - anonymous function - function used for comparisons,
           accepts two arguments, compares them and returns bool true or false.         

class RecursiveSort{
  public static function bubble($list,$compare = false,$p = 0){
    $n = count($list);
    $tmp = null;
    $i = 0;
      $compare = function($a,$b){ return $a > $b; };
    if( $n < 2 ) { return $list;}
    $p = $p === 0 ? $n - 1 : $p - 1; 
    while( $i <= $p - 1 ){ 
        $tmp = $list[$i];
        $list[$i] = $list[$i+1];
        $list[$i+1] = $tmp;
    if ( $p === 1 ){ return $list;}
    return self::bubble( $list,$compare,$p );

Sorting items with RecursiveSort::bubble

RecursiveSort::bubble sorts numbers in ascending order by default. It uses built-in comparison function for this.

Sorting objects in ascending order:


Sorting objects in descending order:

RecursiveSort::bubble(array(9,-22,8,1,35,7,0),function($a,$b){return $a < $b;})

Sorting arrays in descending order:

    return array_sum($a) < array_sum($b);

Sorting object in descending order:

$products = array(
  (object)array("price" => 200),
  (object)array("price" => 700),
  (object)array("price" => 100),
    return $a -> price < $b -> price;

Final thoughts

As mentioned above, due to its asymptotic inefficiency, bubble sort algorithm is usually one of the last choices for sorting large lists. Still, it is fun to play with when testing features of a programming language like PHP.

As for personal gains, learning of not being able to pass an anonymous function as argument to another function in PHP is my take-home from this sorting adventure.

In case you would like to know more about PHP sort, some time ago I wrote an article on sorting objects with it.

This post was updated on 06 Apr 2017 18:18:06

Tags:  php ,  recursion ,  sort 

Author, Copyright and citation


Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.


©Copyright, 2021 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.


Cite this article as:

Wojnowski, Sylwester. "Recursive bubble sort algorithm in PHP." From sWWW - Code For The Web .

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!