Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an SplFixedArray?

Is there a way to perform sorting on integers or strings in an instance of the SplFixedArray class? Is converting to a PHP's array, sorting, and then converting back being the only option?

like image 497
Desmond Hume Avatar asked Jul 21 '13 14:07

Desmond Hume


2 Answers

Firstly, congratulations on finding and using SplFixedArrays! I think they're a highly under-utilised feature in vanilla PHP ...

As you've probably appreciated, their performance is unrivalled (compared to the usual PHP arrays) - but this does come at some trade-offs, including a lack of PHP functions to sort them (which is a shame)!

Implementing your own bubble-sort is a relatively easy and efficient solution. Just iterate through, looking at each consecutive pairs of elements, putting the highest on the right. Rinse and repeat until the array is sorted:

<?php
$arr = new SplFixedArray(10);
$arr[0] = 2345;
$arr[1] = 314;
$arr[2] = 3666;
$arr[3] = 93;
$arr[4] = 7542;
$arr[5] = 4253;
$arr[6] = 2343;
$arr[7] = 32;
$arr[8] = 6324;
$arr[9] = 1;

$moved = 0;
while ($moved < sizeof($arr) - 1) {
    $i = 0;
    while ($i < sizeof($arr) - 1 - $moved) {
        if ($arr[$i] > $arr[$i + 1]) {
            $tmp = $arr[$i + 1];
            $arr[$i + 1] = $arr[$i];
            $arr[$i] = $tmp;
        }
        $i++;

        var_dump ($arr);
    }
    $moved++;
}

It's not fast, it's not efficient. For that you might consider Quicksort - there's documented examples online including this one at wikibooks.org (will need modification of to work with SplFixedArrays).

Seriously, beyond getting your question answered, I truly feel that forcing yourself to ask why things like SplFixedArray exist and forcing yourself to understand what goes on behind a "quick call to array_sort()" (and why it quickly takes a very long time to run) make the difference between programmers and programmers. I applaud your question!

like image 120
wally Avatar answered Nov 14 '22 05:11

wally


Here's my adaptation of bubble sort using splFixedArrays. In PHP 7 this simple program is twice as fast as the regular bubblesort

function bubbleSort(SplFixedArray $a) 
{
   $len = $a->getSize() - 1;
   $sorted = false;

   while (!$sorted) {
    $sorted = true;
    for ($i = 0; $i < $len; $i++)
    {
        $current = $a->offsetGet($i);
        $next = $a->offsetGet($i + 1);

        if ( $next < $current ) {
            $a->offsetSet($i, $next);
            $a->offsetSet($i + 1, $current);
            $sorted = false;
        }
    }
  }

  return $a
}

$starttime = microtime(true);
$array = SplFixedArray::fromArray([3,4,1,3,5,1,92,2,4124,424,52,12]);
$array = bubbleSort($array);

print_r($array->toArray());
echo (microtime(true) - $starttime) * 1000, PHP_EOL;
like image 2
Kearney Taaffe Avatar answered Nov 14 '22 03:11

Kearney Taaffe