我正在尝试在Java中制作timSort的版本,该版本使用array.length <10之后的插入,否则使用合并排序。假设我对insertSort和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);
}
}
答案 0 :(得分:0)
不得不调用一个单独的插入排序,该插入采用了调整后的索引的其他参数