@SuppressWarnings("unchecked")
    private void merge(E[] array, int begin, int center, int end) {
        // cannot create a generic array
        E[] tem = (E[]) new Comparable[end - begin + 1];
        int temindex = 0;
        int i = begin;
        int j = center + 1;
        while (i <= center && j <= end) {
            if (array[i].compareTo(array[j]) <= 0) {
                tem[temindex++] = array[i++];
            } else {
                tem[temindex++] = array[j++];
            }
        }

        while (i <= center) {
            tem[temindex++] = array[i++];
        }

        while (j <= end) {
            tem[temindex++] = array[j++];
        }

        for (temindex = begin; temindex <= end; temindex++) {
            array[temindex] = tem[temindex - begin];
        }
    }