title: Merge sort, where are the sublists saved?
body: <p>I have included the code I am using.</p>
  
  <p>I understand that the start of the code continues splitting the sublists until you have single values. The single values are saved in the variables lefthalf and righthalf and are then merged in sorted order.</p>
  
  <p>How does the code merge two lists of two elements? As I see it these sublists are saved in separate local variables called alist, how are these being merged?</p>
  
  <pre><code>def mergeSort(alist):
  
      if len(alist)&gt;1:
          mid = len(alist)//2
          lefthalf = alist[:mid]
          righthalf = alist[mid:]
  
          mergeSort(lefthalf)
          mergeSort(righthalf)
  
          i=0
          j=0
          k=0
  
          while i &lt; len(lefthalf) and j &lt; len(righthalf):
              if lefthalf[i] &lt; righthalf[j]:
                  alist[k]=lefthalf[i]
                  i=i+1
              else:
                  alist[k]=righthalf[j]
                  j=j+1
              k=k+1
  
  
          while i &lt; len(lefthalf): 
              alist[k]=lefthalf[i]
              i=i+1
              k=k+1
  
  
          while j &lt; len(righthalf):
              alist[k]=righthalf[j]
              j=j+1
              k=k+1
  </code></pre>