Sort a list of unknown values with the least amount of comparisons
Python, score 23069
This uses the Ford–Johnson merge insertion sorting algorithm.
from __future__ import print_function
import sys
def less(x, y):
print(x, '<', y)
sys.stdout.flush()
return bool(int(input()))
def insert(x, a, key=lambda z: z):
if not a:
return [x]
m = len(a)//2
if less(key(x), key(a[m])):
return insert(x, a[:m], key) + a[m:]
else:
return a[:m + 1] + insert(x, a[m + 1:], key)
def sort(a, key=lambda z: z):
if len(a) <= 1:
return a
m = len(a)//2
key1 = lambda z: key(z[-1])
b = sort([insert(x, [y], key) for x, y in zip(a[:m], a[m:2*m])], key=key1)
if len(a) % 2:
b += [[a[-1], None]]
for k in range(m, len(a)):
l, i, (x, y) = max((-i.bit_length(), i, t) for i, t in enumerate(b) if len(t) == 2)
b[:i + 1] = insert([x], b[:i], key=key1) + [[y]]
if len(a) % 2:
b.pop()
return [x for x, in b]
print('a', ' '.join(map(str, sort(range(int(input()))))))