Использование метода Helper в рекурсивной сортировке - PullRequest
0 голосов
/ 10 марта 2019

Я пытаюсь создать версию timSort на Java, которая использует вставку после array.length <10, в противном случае используется сортировка слиянием. Предполагая, что мои вызовы inserttionSort и merge правильные, что мешает следующему избежать попадания в сортировку вставки и правильно выполнить timSorting? </p>

/**
 * 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);
    }
}

1 Ответ

0 голосов
/ 10 марта 2019

Пришлось вызвать отдельную сортировку вставки, которая принимала дополнительные параметры для скорректированных индексов

...