gpt4 book ai didi

algorithm - Bogo-sort平均运行时间解释

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:37:59 25 4
gpt4 key购买 nike

谁能详细解释一下 bogosort 的平均用例运行时间是多少?

算法的伪代码:

while not isInOrder(deck):
shuffle(deck)

最佳答案

n! 个排列,其中只有一个是正确的(假设元素不同)。所以在挥手的意义上,你会期望在大约 O(n!) 次迭代后选择正确的答案。* 但是每个洗牌/检查操作本身都是 O(n)。因此 O(n.n!) 整体。


* 准确地说,您可以建模为 geometrically-distributed random variable带有参数 p = 1/n!。这种变量的预期值为 1/p = n!

关于algorithm - Bogo-sort平均运行时间解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com