是否存在以 O(∞) 排列进行排序的排序算法?
阅读这个问题并通过中提出的各种电话簿排序场景后回答,我发现 BOGO 排序的概念非常有趣。当然,这种类型的排序算法没有任何用处,但它确实在我脑海中提出了一个有趣的问题——它们是否是一种无限不可能完成的排序算法?
换句话说,是否有一个过程可以让人们尝试比较和重新排序一组固定的数据,但永远无法获得实际的排序列表?
这更多的是一个理论/哲学问题,而不是一个实际问题,如果我更像是一名数学家,我可能能够证明/反驳这种可能性。以前有人问过这个问题吗?如果有,可以说什么呢?
After reading this question and through the various Phone Book sorting scenarios put forth in the answer, I found the concept of the BOGO sort to be quite interesting. Certainly there is no use for this type of sorting algorithm but it did raise an interesting question in my mind-- could their be a sorting algorithm that is infinitely impossible to complete?
In other words, is there a process where one could attempt to compare and re-order a fixed set of data and can yet never achieve an actual sorted list?
This is much more of a theoretical/philosophical question than a practical one and if I was more of a mathematician I'd probably be able to prove/disprove such a possibility. Has anyone asked this question before and if so, what can be said about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
[编辑:] 具有有限状态量的确定性过程不会采用“O(无穷大)”,因为它最慢的速度就是遍历所有可能的状态。这包括排序。
[之前更具体的答案:]
不。对于大小为 n 的列表,您只有大小为 n 的状态空间!在其中存储进度(假设排序的整个状态存储在元素的排序中,并且它确实确定性地“做某事”)。
因此,最糟糕的可能行为将在终止之前循环遍历所有可用状态,并且花费与 n 成正比的时间! (冒着混淆问题的风险,必须有一条通过状态的单一路径 - 因为这是“所有状态”,所以您不能让进程从状态 X 移动到 Y,然后再从状态 X 移动到 Z,因为这需要附加状态,或者是不确定的)
[edit:] no deterministic process with a finite amount of state takes "O(infinity)" since the slowest it can be is to progress through all possible states. this includes sorting.
[earlier, more specific answer:]
no. for a list of size n you only have state space of size n! in which to store progress (assuming that the entire state of the sort is stored in the ordering of the elements and it really is "doing something," deterministically).
so the worst possible behaviour would cycle through all available states before terminating and take time proportional to n! (at the risk of confusing matters, there must be a single path through the state - since that is "all the state" you cannot have a process move from state X to Y, and then later from state X to Z, since that requires additional state, or is non-deterministic)
想法 1:
想法 2
你如何定义
O(无穷大)
? Big-O 的正式定义仅指出f(x)=O(g(x))
意味着M*g(x)
是的上限code>f(x)
给定足够大的x
和一些常数M
。通常,当您谈论“无穷大”时,您正在谈论某种无限的限制。因此,在这种情况下,唯一合理的定义是
O(无穷大)
是O(比每个函数都大的函数)
。显然,大于每个函数的函数就是上限。因此,从技术上讲,一切都是“O(infinity)
”想法 3
假设您的意思是 theta 表示法(紧界)...
如果您施加额外的限制,即该算法是智能的(当找到排序的排列时返回)并且列表的每个排列必须在有限的时间内被访问,那么答案是否定的。列表只有
N!
种排列。这种排序算法的上限是有限数上的有限数,它是有限的。Idea 1:
Idea 2
How do you define
O(infinity)
? The formal definition of Big-O merely states thatf(x)=O(g(x))
implies thatM*g(x)
is an upper bound off(x)
given sufficiently largex
and some constantM
.Typically when you talking about "infinity", you are talking about some sort of unbounded limit. So in this case, the only reasonable definition is saying that
O(infinity)
isO(function that's larger than every function)
. Obviously a function that's larger than every function is an upper bound. Thus technically everything is "O(infinity)
"Idea 3
Assuming you mean theta notation (tight bound)...
If you impose the additional restriction that the algorithm is smart (returns when it finds a sorted permutation) and every permutation of the list must be visited in a finite amount of time, then the answer no. There are only
N!
permutations of a list. The upper bound for such a sorting algorithm is then a finite over finite numbers, which is finite.你的问题实际上与排序没有太大关系。保证永远不会完成的算法将是相当乏味的。事实上,即使是一个可能会或可能永远不会完成的算法也会相当乏味。更有趣的是一种算法,它最终可以保证完成,但对于任何函数 F 来说,其相对于输入大小的最坏情况计算时间都不能表示为 O(F(N))在有限时间内计算。我的预感是可以设计出这样的算法,但我不确定如何设计。
Your question doesn't really have much to do with sorting. An algorithm which is guaranteed never to complete would be pretty dull. Indeed, even an algorithm which would might or might not ever complete would be pretty dull. Much more interesting would be an algorithm which would be guaranteed to complete, eventually, but whose worst-case computation time with respect to the size of the input would not be expressible as O(F(N)) for any function F that could itself be computed in bounded time. My hunch would be that such an algorithm could be devised, but I'm not sure how.
这个怎么样:
这是一种排序算法——猴子可能会做的那种算法。是否能保证您会得到一个已排序的列表?我不这么认为!
How about this one:
It's a sorting algorithm -- the kind a monkey might do. Is there any guarantee that you'll arrive at a sorted list? I don't think so!
是的 -
Yes -
更一般地,使比较器要么(a)不可能与输出规范相一致,因此不存在解决方案,要么(b)不可计算(例如,按照数量的顺序对这些(输入,图灵机)对进行排序机器在输入时停止所需的步骤)。
更一般地说,如果您的程序无法在有效输入上停止,则该程序不是解决该输入/输出域问题的算法......这意味着您根本没有算法,或者如果你适当地限制域,你所拥有的只是一种算法。
More generally, make the comparator something that's either (a) impossible to reconcile with the output specification, so that no solution can exist, or (b) uncomputable (e.g., sort these (input, turing machine) pairs in order of the number of steps needed for the machine to halt on the input).
Even more generally, if you have a procedure that fails to halt on a valid input, the procedure is not an algorithm which solves the problem on that input/output domain... which means you don't have an algorithm at all, or that what you have is only an algorithm if you appropriately restrict the domain.
假设您有一个随机的硬币翻转器、无限的算术和无限的有理数。那么答案是肯定的。您可以编写一个排序算法,它有 100% 的机会成功对数据进行排序(因此它确实是一个排序函数),但平均而言需要无限的时间才能完成。
这是 Python 中对此的模拟。
Let's suppose that you have a random coin flipper, infinite arithmetic, and infinite rationals. Then the answer is yes. You can write a sorting algorithm which has 100% chance of successfully sorting your data (so it really is a sorting function), but which on average will take infinite time to do so.
Here is an emulation of this in Python.