18
1
Here's a pretty common pattern for sorting algorithms:
def sort(l):
while not is_sorted(l):
choose indices i, j
assert i < j
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
These algorithms work well because the indices i
and j
are chosen carefully, based on the state of the list l
.
However, what if we couldn't see l
, and just had to choose blindly? How fast could we sort the list then?
Your challenge is to write a function that outputs a random pair of indices, given only the length of l
. Specifically, you must output two indices, i, j
, with 0 <= i < j < len(l)
. Your function should work on any length of list, but it will be scored on a list of length 100.
Your score is the mean number of index choices necessary to sort a uniformly randomly shuffled list according to the above pattern, where the indices are chosen according to your function.
I will score submissions, taking the mean number of index choices over 1000 trials on a uniformly randomly shuffled list of length 100 with no repeated entries.
I reserve the right to run less trials if the submission is clearly non-competitive or does not terminate, and I will run more trials to differentiate the top competitors to find a single winner. If multiple top submissions remain within the margin of error at the limit of my computational resources, I will declare the earlier submission the winner, until further computational resources can be brought to bear.
Here's an example scoring program, in Python:
import random
def is_sorted(l):
for x in range(len(l)-1):
if l[x] > l[x+1]:
return False
return True
def score(length, index_chooser):
steps = 0
l = list(range(length))
random.shuffle(l)
while not is_sorted(l):
i, j = index_chooser(length)
assert (i < j)
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
steps += 1
return steps
Your function may not maintain any mutable state, interact with global variables, affect the list l
, etc. Your function's only input must be the length of the list l
, and it must output a ordered pair of integers in the range [0, len(l)-1]
(or appropriate for your language's list indexing). Feel free to ask whether something's allowed in the comments.
Submissions may be in any free-to-use language. Please include a scoring harness if one has not already been posted for your language. You may post a provisional score, but I will leave a comment with the official score.
Scoring is the mean number of steps to a sorted list on a uniformly randomly shuffled list of length 100. Good luck.
If we can't maintain a mutable state, then is the submission just based on whatever distribution we implement? – Jo King – 2018-10-17T04:00:07.487
2@JoKing Indeed - your submission is a distribution – isaacg – 2018-10-17T04:03:47.270
2Why don't you allow mutable state? Allowing it means that submissions can better fine-tune their algorithms, as opposed to hoping that the right items get picked. – Nathan Merrill – 2018-10-17T04:07:42.060
3
@NathanMerrill If mutable state were allowed, the winner would just be a sorting network which is already a well studied problem.
– Anders Kaseorg – 2018-10-17T04:13:08.2931Isn't finding efficient sorting networks of a large size a really hard problem? As long as N > 20, I don't see that being an issue. – Nathan Merrill – 2018-10-17T04:19:29.683
But a random number generator is a mutable state? Am I right? – tsh – 2018-10-17T05:03:58.940
1@tsh A standard random number generator is allowed, as exception to the mutable state rule. Don't use this to transfer information between iterations. – isaacg – 2018-10-17T05:34:26.370
3@NathanMerrill If you want to post that question, feel free. It's not this question, however. – isaacg – 2018-10-17T05:34:49.763
@isaacg I wasn't proposing you actually change the challenge, I was asking what your reasoning was :) – Nathan Merrill – 2018-10-17T06:23:22.723
3@NathanMerrill Oh, sure. The challenge "Design the best sorting network", while an interesting question, has been studied a lot in the CS research world. As a result, the best submissions would probably just consist of implementations of research papers, such as Batcher's bitonic sort. The question I've asked here is original as far as I know, and so should have more room for innovation. – isaacg – 2018-10-17T06:31:20.953
1@Lynn Switched, thanks. – isaacg – 2018-10-18T00:01:01.173