Implement an algorithm to merge an arbitrary number of sorted lists into one sorted list. The aim is to create the smallest working programme, in whatever language you like.
For example:
input: ((1, 4, 7), (2, 5, 8), (3, 6, 9))
output: (1, 2, 3, 4, 5, 6, 7, 8, 9)
input: ((1, 10), (), (2, 5, 6, 7))
output: (1, 2, 5, 6, 7, 10)
Note: solutions which concatenate the input lists then use a language-provided sort function are not in-keeping with the spirit of golf, and will not be accepted:
sorted(sum(lists,[])) # cheating: out of bounds!
Apart from anything else, your algorithm should be (but doesn't have to be) a lot faster!
Clearly state the language, any foibles and the character count. Only include meaningful characters in the count, but feel free to add whitespace to the code for artistic / readability purposes.
To keep things tidy, suggest improvement in comments or by editing answers where appropriate, rather than creating a new answer for each "revision".
EDIT: if I was submitting this question again, I would expand on the "no language provided sort" rule to be "don't concatenate all the lists then sort the result". Existing entries which do concatenate-then-sort are actually very interesting and compact, so I won't retro-actively introduce a rule they break, but feel free to work to the more restrictive spec in new submissions.
Inspired by Combining two sorted lists in Python
C#
I don't think you can get much better than @Sykora's response, here, for Python.
Changed to handle your inputs:
For the actual function, 59 characters, or the 52 in reduced version:
This also has the benefit of using a tested and true implementation built into Python
Edit: Removed the semi-colons (thanks @Douglas).
Common Lisp already has a
merge
function for general sequences in the language standard, but it only works on two sequences. For multiple lists of numbers sorted ascendingly, it can be used in the following function (97 essential characters).edit: Revisiting after some time: this can be done in one line:
This has 79 essential characters with meaningful names, reducing those to a single letter, it comes out at 61:
OCaml in 42 characters:
I think I should get extra credit for 42 exactly?
Python: 113 characters
EDIT: seeing as talk of performance has come up in a few places, I'll mention that I think this is pretty efficient implementation, especially as the lists grow. I ran three algorithms on 10 lists of sorted random numbers:
sorted(sum(lists, []))
(Built-in)EDIT2: (JFS)
The figure's labels:
merge_26
--heapq.merge()
from Python 2.6 stdlibmerge_alabaster
-- the above code (labeledMerge
on the above figure)sort_builtin
--L = sum(lists,[]); L.sort()
Input data are
[f(N) for _ in range(10)]
, wheref()
is:NOTE:
merge_alabaster()
doesn't work forN > 100
due toRuntimeError: "maximum recursion depth exceeded"
.To get Python scripts that generated this figure, type:
Conclusion: For reasonably large lists the built-in sort shows near linear behaviour and it is the fastest.
resubmitted
Python - 74 chars (counting whitespace and newlines)
i
is input as list of listsUsage: