讓我們假設你有一個隨機投幣器,無限算術和無限理性。那麼答案是肯定的。你可以編寫一個排序算法,它有100%的成功排序數據的機會(所以它真的是一個排序功能),但平均來說,這將花費無限的時間。
下面是Python中對此的模擬。
# We'll pretend that these are true random numbers.
import random
import fractions
def flip():
return 0.5 < random.random()
# This tests whether a number is less than an infinite precision number in the range
# [0, 1]. It has a 100% probability of returning an answer.
def number_less_than_rand (x):
high = fractions.Fraction(1, 1)
low = fractions.Fraction(0, 1)
while low < x and x < high:
if flip():
low = (low + high)/2
else:
high = (low + high)/2
return high < x
def slow_sort (some_array):
n = fractions.Fraction(100, 1)
# This loop has a 100% chance of finishing, but its average time to complete
# is also infinite. If you haven't studied infinite series and products, you'll
# just have to take this on faith. Otherwise proving that is a fun exercise.
while not number_less_than_rand(1/n):
n += 1
print n
some_array.sort()
'while(true){}'will do。 – Vlad
可能的重複[是否有任何更糟糕的排序算法比Bogosort(又名猴排序)?](http://stackoverflow.com/questions/2609857/are-there-any-worse-sorting-algorithms-than-bogosort-aka -monkey-sort) –
@Vlad。這不是一個算法。對不起,沒有要點。 – RLH