比較しないソートの作成
Posted feedbacks - Python
これでいいのかな。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def sort(min, max, a):
counts = [0] * (max - min + 1)
for x in a:
counts[x - min] += 1
result = []
for i, count in enumerate(counts):
for _ in xrange(count):
result.append(i + min)
return result
def main():
print sort(-1, 10, [-1, 9, 4, 8, 9, 6, 3, 9, 5, 2])
if __name__ == '__main__':
main()
|
listを何回も作っているのがダサいです。アルゴリズムとしてはradix sort系です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | def nocmpsort(min, max, count, data):
"""
>>> nocmpsort(min=-1, max=10, count=10,\
data=[-1, 9, 4, 8, 9, 6, 3, 9, 5, 2])
[-1, 2, 3, 4, 5, 6, 8, 9, 9, 9]
"""
latitude = max - min
base = 0
x = 1
while latitude > x:
x = x << 1
base += 1
def encode(e):
return e - min
def decode(d):
return d + min
def nocmpsort_binary(data):
"""
>>> base = 5
>>> nocmpsort_binary([0, 10, 5, 9, 10, 7, 4, 10, 6, 3])
[0, 3, 4, 5, 6, 7, 9, 10, 10, 10]
"""
bin = list()
for i in range( 2 << base):
bin.append(0)
for d in data:
bin[d]+=1
r = list()
for i, count in enumerate(bin):
while count > 0:
r.append(i)
count -=1
return r
return map(decode, nocmpsort_binary(map(encode, data)))
|
再帰をつかった基数2のバージョン
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | def nocmpsort(min, max, count, data):
"""
>>> nocmpsort(min=-1, max=10, count=10,\
data=[-1, 9, 4, 8, 9, 6, 3, 9, 5, 2])
[-1, 2, 3, 4, 5, 6, 8, 9, 9, 9]
"""
latitude = max - min
base = 0
x = 1
while latitude > x:
x = x << 1
base += 1
def encode(e):
return e - min
def decode(d):
return d + min
def nocmpsort_binary(data):
"""
>>> base = 5
>>> nocmpsort_binary([0, 10, 5, 9, 10, 7, 4, 10, 6, 3])
[0, 3, 4, 5, 6, 7, 9, 10, 10, 10]
"""
def binary(base, data):
if base == -1:
r = list(data)
return r
assert base > -1
small = list()
big = list()
mask = 1 << base
for d in data:
if mask & d:
big.append(d)
else:
small.append(d)
r = binary(base - 1, small)
r.extend(binary(base - 1, big))
return r
return binary(base, data)
return map(decode, nocmpsort_binary(map(encode, data)))
if __name__ == "__main__":
import doctest
doctest.testmod()
|



sweetie089 #6628() Rating3/3=1.00
[ reply ]