protected long crossInversions(int[] values, int l, int r, int mid) {
        int pl = l;
        int pr = mid + 1;
        long inversion = 0;
        int i;
        for (i = l; i <= r && pl <= mid && pr <= r; ++i) {
            if (values[pl] < values[pr]) {
                tmp[i] = values[pl];
                pl ++;
            } else {
                tmp[i] = values[pr];
                pr ++;
                inversion += (mid - pl + 1);
            }
        }
        if (pl <= mid ) {
            for (; i <= r; ++i, ++pl) {
                tmp[i] = values[pl];
            }
        } else if (pr <= r){
            for (; i <= r; ++i, ++pr) {
                tmp[i] = values[pr];
            }
        }
        
        for (i = l ; i <= r; ++i) {
            values[i] = tmp[i];
        }
        return inversion;
    }