如何在java中使用泛型实现递归mergeSort?

如何在java中使用泛型实现递归mergeSort?,java,generics,recursion,mergesort,Java,Generics,Recursion,Mergesort,我正在实现一个mergeSort函数。我理解分而治之的逻辑,但实际的合并部分让我困惑。这是一道过去的作业题,但我正在努力理解它 /** * Implement merge sort. * * It should be: * stable * Have a worst case running time of: * O(n log n) *

我正在实现一个mergeSort函数。我理解分而治之的逻辑,但实际的合并部分让我困惑。这是一道过去的作业题,但我正在努力理解它

       /**
         * Implement merge sort.
         *
         * It should be:
         * stable

         * Have a worst case running time of:
         *  O(n log n)
         *
         * And a best case running time of:
         *  O(n log n)
         *
         * You can create more arrays to run mergesort, but at the end,
         * everything should be merged back into the original T[]
         * which was passed in.
         *
         * ********************* IMPORTANT ************************
         * FAILURE TO DO SO MAY CAUSE ClassCastException AND CAUSE
         * YOUR METHOD TO FAIL ALL THE TESTS FOR MERGE SORT
         * ********************************************************
         *
         * Any duplicates in the array should be in the same relative position      
         * after sorting as they were before sorting.
         *
         * @throws IllegalArgumentException if the array or comparator is null
         * @param <T> data type to sort
         * @param arr the array to be sorted
         * @param comparator the Comparator used to compare the data in arr
         */
/**
*实现合并排序。
*
*应该是:
*马厩
*最坏情况下的运行时间为:
*O(n日志n)
*
*最佳运行时间为:
*O(n日志n)
*
*您可以创建更多数组来运行mergesort,但最后,
*所有内容都应合并回原始T[]
*这是通过的。
*
*****************************重要************************
*否则可能导致ClassCastException和
*您的方法无法通过合并排序的所有测试
* ********************************************************
*
*数组中的任何重复项都应位于相同的相对位置
*排序后,就像排序前一样。
*
*如果数组或比较器为空,@将引发IllegalArgumentException
*@param要排序的数据类型
*@param arr要排序的数组
*@param comparator用于比较arr中数据的比较器
*/
对于此方法,不能更改参数public、static和泛型。我不知道如何做递归合并函数

public static <T> void mergesort(T[] arr, Comparator<T> comparator) {
    if (arr == null || comparator == null) {
        throw new IllegalArgumentException("Null arguments were passed.");
    }
    if (arr.length >= 2) {
        //Midpoint from which we will split the array.
        int middle = arr.length / 2;
        //Each half of the split array
        T[] left = (T[]) new Object[middle];
        T[] right = (T[]) new Object[arr.length - middle];
        //Copy items from original into each half
        for (int i = 0; i < middle; i++) {
            left[i] = arr[i];
        }
        for (int i = middle; i < length; i++) {
            right[i] = arr[i];
        }
        //Keep splitting until length is 1
        mergesort(left, comparator);
        mergesort(right, comparator);
        //merge each array back into original which would now be sorted.
        merge(left, right, middle, arr, comparator);
        merge(right, middle, arr, comparator);

    }

}

private static <T> T[] merge(T[] left, T[] right, int middle, T[] arr,
                             Comparator<T>
        comparator) {
    int i = 1, j = middle + 1, k = 1;
    while (i <= middle && j <= arr.length) {
        arr[k++] = (comparator.compare(arr[k], partioned[i]) < 0)
                ? arr[j++] : partioned[i++];
    }
    while (i <= middle) {
        arr[k++] = partioned[k++];
    }
}
publicstaticvoidmergesort(T[]arr,Comparator-Comparator){
if(arr==null | |比较器==null){
抛出新的IllegalArgumentException(“传递了Null参数”);
}
如果(arr.length>=2){
//我们将从中拆分阵列的中点。
int middle=arr.length/2;
//分割阵列的每一半
T[]左=(T[])新对象[中间];
T[]right=(T[])新对象[arr.length-middle];
//将项目从原件复制到每一半
for(int i=0;i然而(i这里有一个可能的解决方案。我不记得是否有更好的方法来做到这一点,因为我通常只使用Collections.sort()

注意,不需要返回值,因为原始数组的内容将被修改

<>也不需要传递中间索引。

private static <T> void merge(T[] left, T[] right, T[] dest) {
  if (left.length + right.length != dest.length)
    throw new IllegalArgumentException(
                    "left length + right length must equal destination length");
  int leftIndex = 0;
  int rightIndex = 0;
  int destIndex = 0;
  while (destIndex < dest.length) {
    if (leftIndex >= left.length) //no more entries in left array, use right
      dest[destIndex++] = right[rightIndex++];
    else if (rightIndex >= right.length) // no more entries in right array, use left
      dest[destIndex++] = left[leftIndex++];
    else if (left[leftIndex] < right[rightIndex]) //otherwise pick the lower value
      dest[destIndex++] = left[leftIndex++];
    else 
      dest[destIndex++] = right[rightIndex++];
  }
}
私有静态无效合并(T[]左,T[]右,T[]目标){
如果(left.length+right.length!=目标长度)
抛出新的IllegalArgumentException(
“左长度+右长度必须等于目标长度”);
int leftIndex=0;
int rightIndex=0;
int destIndex=0;
while(目标索引<目标长度){
如果(leftIndex>=left.length)//左数组中没有更多条目,请使用right
dest[destinex++]=右[rightIndex++];
否则,如果(rightIndex>=right.length)//右数组中没有更多条目,请使用left
dest[destIndex++]=左[leftIndex++];
else if(left[leftIndex]
在不详细介绍合并排序实现的情况下,您的私有合并逻辑不是递归的,因此您可以在公共方法中实现它。