简体   繁体   English

如何对SplFixedArray排序?

[英]How to sort an SplFixedArray?

Is there a way to perform sorting on integers or strings in an instance of the SplFixedArray class? 有没有一种方法可以对SplFixedArray类实例中的整数或字符串进行排序? Is converting to a PHP's array , sorting, and then converting back being the only option? 是转换为PHP的array ,进行排序,然后再转换回为唯一选择吗?

Firstly, congratulations on finding and using SplFixedArrays! 首先,恭喜您找到并使用了SplFixedArrays! I think they're a highly under-utilised feature in vanilla PHP ... 我认为它们是香草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)! 您可能已经意识到,它们的性能是无与伦比的(与普通的PHP数组相比)-但这确实需要权衡取舍,包括缺乏对它们进行排序的PHP函数(这很可惜)!

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). 对于你可能会考虑快速排序-有记录在线的例子包括本在wikibooks.org (需要与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. 认真地说,除了让您的问题得到回答之外,我真的感到自己要强迫自己问为什么存在SplFixedArray之类的事物,并强迫自己了解“快速调用array_sort() ”后发生了什么(以及为什么快速花很长时间才能完成)。运行)使程序员与程序员之间有所不同。 I applaud your question! 我欢迎您的提问!

Here's my adaptation of bubble sort using splFixedArrays. 这是我使用splFixedArrays进行冒泡排序的改编。 In PHP 7 this simple program is twice as fast as the regular bubblesort 在PHP 7中,这个简单的程序的速度是常规bubbleort的两倍

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM