1: public static void merge(Elem array[], Elem temp[], int low, int mid, int high) {
   2:     
   3:     int insertindex = low;
   4:     int lowindex = low;
   5:     int highindex = mid+1;
   6:     
   7:     while (insertindex <= high) {
   8:         if (lowindex > mid)
   9:             temp[insertindex++] = array[highindex++];
  10:         else if (highindex > high) 
  11:             temp[insertindex++] = array[lowindex++];
  12:         else if (array[lowindex].key() < array[highindex].key())
  13:             temp[insertindex++] = array[lowindex++];
  14:         else
  15:             temp[insertindex++] = array[highindex++];
  16:     }
  17:     for (insertindex = low; insertindex <= high; insertindex++)
  18:         array[insertindex] = temp[insertindex];
  19: }
  20: 
  21: 
  22: public static void mergesort(Elem array[], Elem temp[], int low, int high) {
  23:     int mid;
  24:     
  25:     if (low >= high)
  26:         return;
  27:     
  28:     mid = (low + high) / 2;
  29:     
  30:     mergesort(array, temp, low, mid);
  31:     mergesort(array, temp, mid+1, high);
  32:     
  33:     merge(array, temp, low, mid, high);
  34: }
  35: