Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Python: 113 characters</strong></p> <pre><code>def m(c,l): try: c += [l[min((m[0], i) for i,m in enumerate(l) if m)[1]].pop(0)] return m(c,l) except: return c # called as: &gt;&gt;&gt; m([], [[1,4], [2,6], [3,5]]) [1, 2, 3, 4, 5, 6] </code></pre> <p><strong>EDIT:</strong> 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:</p> <ul> <li>my solution (<em>Merge</em>)</li> <li><code>sorted(sum(lists, []))</code> (<em>Built-in</em>)</li> <li><a href="https://stackoverflow.com/questions/464960/code-golf-combining-multiple-sorted-lists-into-a-single-sorted-list#465044">Deestan's solution</a> which sorted in a different way (<em>Re-implement</em>)</li> </ul> <p><img src="https://i622.photobucket.com/albums/tt302/gefafwisp/merge_gr.png" alt="List merge performance"></p> <p><strong>EDIT</strong>2: (JFS)</p> <p>The figure's labels:</p> <ul> <li><code>merge_26</code> -- <code>heapq.merge()</code> from Python 2.6 stdlib </li> <li><code>merge_alabaster</code> -- the above code (labeled <code>Merge</code> on the above figure)</li> <li><code>sort_builtin</code> -- <code>L = sum(lists,[]); L.sort()</code></li> <li><a href="https://stackoverflow.com/questions/464960/code-golf-combining-multiple-sorted-lists-into-a-single-sorted-list#465044">Deestan's solution</a> is O(N**2) so it is excluded from the comparison (all other solutions are O(N) (for the input provided))</li> </ul> <p>Input data are <code>[f(N) for _ in range(10)]</code>, where <code>f()</code> is:</p> <pre><code>max_ = 2**31-1 def f(N): L = random.sample(xrange(max_), n) L.sort() return L f.__name__ = "sorted_random_%d" % max_ </code></pre> <p><img src="https://i.stack.imgur.com/6Ngpo.png" alt="Performance data Nmax=2**16"> NOTE: <code>merge_alabaster()</code> doesn't work for <code>N &gt; 100</code> due to <code>RuntimeError: "maximum recursion depth exceeded"</code>.</p> <p>To get <a href="http://gist.github.com/51074" rel="nofollow noreferrer">Python scripts that generated this figure</a>, type:</p> <pre><code>$ git clone git://gist.github.com/51074.git </code></pre> <p>Conclusion: For reasonably large lists the built-in sort shows near linear behaviour and it is the fastest. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload