private static<T extends Comparable<? super T>> void merge(T[] data, T[] tmp, int leftPos, int rightPos, int rightEnd) {
        int leftEnd = rightPos - 1;
        int tmpPos = leftPos;
        int leftPos2 = leftPos;
        while (leftPos <= leftEnd && rightPos <= rightEnd) {
            if (data[leftPos].compareTo(data[rightPos]) > 0) {
                tmp[tmpPos++] = data[rightPos++];
            } else {
                tmp[tmpPos++] = data[leftPos++];
            }
        }
        
        while (leftPos <= leftEnd) {
            tmp[tmpPos++] = data[leftPos++];
        }
        
        while (rightPos <= rightEnd) {
            tmp[tmpPos++] = data[rightPos++];
        }
        
        for (; rightEnd >= leftPos2; rightEnd--) {
            data[rightEnd] = tmp[rightEnd];
        }
    }