あなたがランダムなコインフリッパー、無限の算術、そして無限の有理数を持っていると仮定しましょう。答えはイエスです。データを正常に並べ替える可能性が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()