Add Tim's worst case scenario.

Revert to using whrandom so it will work with older versions of Python.
This commit is contained in:
Guido van Rossum 1998-05-26 15:05:12 +00:00
parent 7462942b69
commit 16653cb273
1 changed files with 11 additions and 6 deletions

View File

@ -7,7 +7,7 @@ See tabulate() for output format.
import sys import sys
import time import time
import random import whrandom
import marshal import marshal
import tempfile import tempfile
import operator import operator
@ -23,7 +23,7 @@ def randrange(n):
except IOError: except IOError:
result = [] result = []
for i in range(n): for i in range(n):
result.append(random.random()) result.append(whrandom.random())
try: try:
try: try:
fp = open(fn, "wb") fp = open(fn, "wb")
@ -44,7 +44,7 @@ def randrange(n):
##assert len(result) == n ##assert len(result) == n
# Shuffle it a bit... # Shuffle it a bit...
for i in range(10): for i in range(10):
i = random.randint(0, n-1) i = whrandom.randint(0, n-1)
temp = result[:i] temp = result[:i]
del result[:i] del result[:i]
temp.reverse() temp.reverse()
@ -75,10 +75,12 @@ def tabulate(r):
/sort: ascending data /sort: ascending data
~sort: many duplicates ~sort: many duplicates
-sort: all equal -sort: all equal
!sort: worst case scenario
""" """
fmt = ("%2s %6s" + " %6s"*5) cases = ("*sort", "\\sort", "/sort", "~sort", "-sort", "!sort")
print fmt % ("i", "2**i", "*sort", "\\sort", "/sort", "~sort", "-sort") fmt = ("%2s %6s" + " %6s"*len(cases))
print fmt % (("i", "2**i") + cases)
for i in r: for i in r:
n = 1<<i n = 1<<i
L = randrange(n) L = randrange(n)
@ -97,6 +99,9 @@ def tabulate(r):
del L del L
L = map(abs, [-0.5]*n) L = map(abs, [-0.5]*n)
doit(L) # -sort doit(L) # -sort
L = range(n/2-1, -1, -1)
L[len(L):] = range(n/2)
doit(L) # !sort
print print
def main(): def main():
@ -129,7 +134,7 @@ def main():
y = (y^h^d) & 255 y = (y^h^d) & 255
h = h>>8 h = h>>8
z = (z^h^d) & 255 z = (z^h^d) & 255
random.seed(x, y, z) whrandom.seed(x, y, z)
r = range(k1, k2+1) # include the end point r = range(k1, k2+1) # include the end point
tabulate(r) tabulate(r)