private static void mergeSort(final int[] refX,final int[] refY,final int[] srcX,final int[] dstX,final int[] srcY,final int[] dstY,final int low,final int high){
  final int length=high - low;
  if (length <= INSERTION_SORT_THRESHOLD) {
    dstX[low]=refX[low];
    dstY[low]=refY[low];
    for (int i=low + 1, j=low, x, y; i < high; j=i++) {
      x=refX[i];
      y=refY[i];
      while (dstX[j] > x) {
        dstX[j + 1]=dstX[j];
        dstY[j + 1]=dstY[j];
        if (j-- == low) {
          break;
        }
      }
      dstX[j + 1]=x;
      dstY[j + 1]=y;
    }
    return;
  }
  final int mid=(low + high) >> 1;
  mergeSort(refX,refY,dstX,srcX,dstY,srcY,low,mid);
  mergeSort(refX,refY,dstX,srcX,dstY,srcY,mid,high);
  if (srcX[high - 1] <= srcX[low]) {
    final int left=mid - low;
    final int right=high - mid;
    final int off=(left != right) ? 1 : 0;
    System.arraycopy(srcX,low,dstX,mid + off,left);
    System.arraycopy(srcX,mid,dstX,low,right);
    System.arraycopy(srcY,low,dstY,mid + off,left);
    System.arraycopy(srcY,mid,dstY,low,right);
    return;
  }
  if (srcX[mid - 1] <= srcX[mid]) {
    System.arraycopy(srcX,low,dstX,low,length);
    System.arraycopy(srcY,low,dstY,low,length);
    return;
  }
  for (int i=low, p=low, q=mid; i < high; i++) {
    if ((q >= high) || ((p < mid) && (srcX[p] <= srcX[q]))) {
      dstX[i]=srcX[p];
      dstY[i]=srcY[p];
      p++;
    }
 else {
      dstX[i]=srcX[q];
      dstY[i]=srcY[q];
      q++;
    }
  }
}

private static void mergeSort ( final int [ ] refX , final int [ ] refY , final int [ ] srcX , final int [ ] dstX , final int [ ] srcY , final int [ ] dstY , final int low , final int high ) { final int length = high - low ; if ( length <= INSERTION_SORT_THRESHOLD ) { dstX [ low ] = refX [ low ] ; dstY [ low ] = refY [ low ] ; for ( int i = low + 1 , j = low , x , y ; i < high ; j = i ++ ) { x = refX [ i ] ; y = refY [ i ] ; while ( dstX [ j ] > x ) { dstX [ j + 1 ] = dstX [ j ] ; dstY [ j + 1 ] = dstY [ j ] ; if ( j -- == low ) { break ; } } dstX [ j + 1 ] = x ; dstY [ j + 1 ] = y ; } return ; } final int mid = ( low + high ) > > 1 ; mergeSort ( refX , refY , dstX , srcX , dstY , srcY , low , mid ) ; mergeSort ( refX , refY , dstX , srcX , dstY , srcY , mid , high ) ; if ( srcX [ high - 1 ] <= srcX [ low ] ) { final int left = mid - low ; final int right = high - mid ; final int off = ( left != right ) ? 1 : 0 ; System . arraycopy ( srcX , low , dstX , mid + off , left ) ; System . arraycopy ( srcX , mid , dstX , low , right ) ; System . arraycopy ( srcY , low , dstY , mid + off , left ) ; System . arraycopy ( srcY , mid , dstY , low , right ) ; return ; } if ( srcX [ mid - 1 ] <= srcX [ mid ] ) { System . arraycopy ( srcX , low , dstX , low , length ) ; System . arraycopy ( srcY , low , dstY , low , length ) ; return ; } for ( int i = low , p = low , q = mid ; i < high ; i ++ ) { if ( ( q >= high ) || ( ( p < mid ) && ( srcX [ p ] <= srcX [ q ] ) ) ) { dstX [ i ] = srcX [ p ] ; dstY [ i ] = srcY [ p ] ; p ++ ; } else { dstX [ i ] = srcX [ q ] ; dstY [ i ] = srcY [ q ] ; q ++ ; } } }
src is the source array that starts at index 0 dest is the ( possibly larger ) array destination with a possible offset low is the index in dest to start sorting high is the end index in dest to end sorting
