PHP 中的计数排序
在一个 PHP 项目中,我有一些数据想要使用线性时间、简单计数排序进行排序:
$ar = array(7, 2, 0, 3, 8, 0, 12, 7, 6, 7);
$count = array();
foreach ($ar as $v)
$count[$v]++;
$sorted = array();
foreach ($count as $v => $c)
for ($i = 0; $i < $c; $i++)
$sorted[] = $v;
问题是上面的方法显然不起作用。 php 数组的工作方式更像是哈希图而不是数组。可以通过在最终循环之前插入 ksort($count)
来使代码正常工作,但是 ksort
的运行时间为 O(nlogn)
,这会破坏整个点。
有没有办法在php中进行线性时间排序?也许使用一些 array()
参数,或者一些完全不同的结构?
In a PHP project I have some data I want to sort using a linear time, simple counting sort:
$ar = array(7, 2, 0, 3, 8, 0, 12, 7, 6, 7);
$count = array();
foreach ($ar as $v)
$count[$v]++;
$sorted = array();
foreach ($count as $v => $c)
for ($i = 0; $i < $c; $i++)
$sorted[] = $v;
The problem is that the above obviously doesn't work. The php array works more like a hashmap than an array. The code can be made to work by inserting ksort($count)
before the final loop, but ksort
runs in O(nlogn)
which destroys the entire point.
Is there any way to do a linear time sort in php? Perhaps using some paramter to array()
, or some entirely different structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您没有正确遵循算法。这是 O(n)。
另请参阅 array_count_values(),或者计算计数循环内的最小值和最大值。
You didn't follow the algorithm correctly. This is O(n).
also, see array_count_values(), or alternatively compute the min and max inside the counting loop.
批准的答案是错误的。正确的:
Approved answer is wrong. Correct:
如果我正确理解你的问题和评论,那么只使用 sort($count) 就可以了,不是吗?
结果:
但我想知道 foreach($ar as $v)$count[$v]++; 是什么?确实……没有什么意义……
If i understand your question AND comment correctly, just using sort($count) would work no?
Result:
But i'm wondering what the foreach($ar as $v)$count[$v]++; does... doesn't really make sense...
在代码中添加一些注释,以向您展示为什么这没有执行您认为应该执行的操作。
这只是根据数字在第一个数组中的位置将数字分组在一起。
Adding some comments to the code to show you why this doesn't do what you think it should do.
This simply groups numbers together based on their location in the first array.
要在它自己的变量(
$sorted
)中获取已排序的$ar
,这非常简单:这让我认为您的问题和评论并没有给出完整的图片。
编辑:现在,在您澄清您想要实现特定算法之后(并且您实际上已经得到了一个答案,该答案显示了首先实现它的一些错误点),我认为值得关注另一个算法你的问题的一个方面:
你正在比较两种(理论)算法的复杂性,但你忽略了算法的实现方式。
PHP 的
sort()
- 即使基于“糟糕的”快速排序,也会比您自己的 PHP 用户代码实现的其他算法高出。您刚刚在这里比较了错误的参数。当您将内置 PHP 函数与用户代码中的某些函数进行比较时,函数的复杂性并没有说明太多。
To get the sorted
$ar
in a variable of it's own ($sorted
), it's pretty trivial:Which makes me think that your question and comment is not giving the whole picture.
Edit: Now after you have clarified that you wanted to implement a specific algorithm (and you actually got an answer already that shows some points that were wrong implementing it first), I think it's worth to focus on another aspect of your question:
You're comparing the complexity of two (theoretical) algorithms, but you're leaving aside how the algorithms are implemented.
PHP's
sort()
- even based on "bad" quicksort, will outrun your own PHP usercode implementation of some other algorithm by numbers.You just have compared the wrong parameters here. The complexity of a function does not says much when you compare a build-in PHP function with some function in your user-code.