private static void merge(int[ ] list, int first, int n1, int n2)
  {
    int[ ] temp = new int[n1+n2]; // Create temp array
    int copied  = 0; // Number of elements copied from list to temp
    int copied1 = 0; // Number copied from the first half of list
    int copied2 = 0; // Number copied from the second half of list
    int i;           // Array index to copy from temp back into list
    
    // Merge elements, copying from two halves of list to the temp array.
    while ((copied1 < n1) && (copied2 < n2))
    {
      if (list[first + copied1] < list[first + n1 + copied2])
        temp[copied++] = list[first + (copied1++)];
      else
        temp[copied++] = list[first + n1 + (copied2++)];
    }
    
    // Copy any remaining entries in the left and right subarrays.
    while (copied1 < n1)
      temp[copied++] = list[first + (copied1++)];
    
    while (copied2 < n2)
      temp[copied++] = list[first + n1 + (copied2++)];
    
    // Copy from temp back to the list array.
    for (i = 0; i < n1+n2; i++)
      list[first + i] = temp[i];
  }