0

我正在尝试在 Java 中制作一个 timSort 版本,它在 array.length < 10 之后使用插入,否则使用合并排序。假设我对insertionSort 和merge 的调用是正确的,那么是什么阻止了以下内容的插入排序和正确的timSorting?

/**
 * timSort is a generic sorting method that sorts an array of Comparable data
 * using the TimSort algorithm. Make sure this method is public so that we can
 * test it.
 * 
 * @param data The array of data to be sorted
 * @param      <E> The Generic Element.
 */
public static <E extends Comparable<E>> void timSort(E[] data)
{
    timSortHelper(data, 0, data.length - 1);


}

/**
 * timSortHelper is a generic sorting method that sorts a sub-array array of
 * Comparable data using the TimSort algorithm. This method should be public for
 * testing purposes but would normally be private.
 * 
 * Ranges are specified with the parameters left and right, which are inclusive.
 * 
 * @param       <E> The Generic Element.
 * @param data  The array of data to sort
 * @param left  The index of the left-most position to sort
 * @param right The index of the right most position to sort
 */
public static <E extends Comparable<E>> void timSortHelper(E[] data, int left, int right)
{
    // General Case: The sublist has at least one item in it.

    if ((right - left) >= 1)
    {

        int middle1 = (left + right) / 2;
        int middle2 = middle1 + 1;
        if (data.length < 10)
        {
            insertionSort(data);
        }
        else
        {
            timSortHelper(data, left, middle1);
            timSortHelper(data, middle2, right);

        }
        merge(data, left, middle1, middle2, right);
    }
}
4

1 回答 1

0

必须调用一个单独的插入排序,该排序采用调整后索引的附加参数

于 2019-03-10T18:42:25.187 回答