title: Implementing k-way merge sort
body: <pre><code>import random as r
  import heapq as h
  
  n = 4
  yo = 0
  jk = []
  o = []
  l = 0
  
  for ih in range(0, 5):
      jk = []
  
      for i in range(n):
          t = r.randint(0, 10)
          jk.append(t)
  
      jk.sort()    
      o.append(jk)
  
  for i in o:
      print(i)
  
  def ksort(l, a, b, c):
      print('c=', end='')
      print(c)
      print()
  
      for i in range(len(a)):
          if a[i] &gt; (len(l[i])):
              a[i] = -3
  
      for i in range(len(l)):
          if a[i] &gt;= 0:
              b[i] = l[i][a[i]]
  
      h.heapify(b)
  
      #print(a)
      #print(b)
  
      c.append(b[0])
  
      yu = b[0]
      op = 0
      for i in range(len(l)):
          if a[i] &gt;= 0 and (l[i][a[i]] == yu) and op == 0:
              a[i] += 1
              b[i] = l[i][a[i] + 1]
              op = 1
      ksort(l, a, b, c)
  
  ju = ksort(o, [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [])
  
  ju
  </code></pre>
  <p><img src="https://i.stack.imgur.com/8Jrh1.png" alt="enter image description here" /></p>
  <p>I am using the above code to implement k-way merge sort. The lengths that I have taken for each of the individual lists are 4. But after a certain point, it crashes due to the out of range error. Can anyone suggest anything?</p>