Ошибка при тестировании алгоритма быстрой сортировки - PullRequest
0 голосов
/ 14 апреля 2020

Получение java .lang.ArrayIndexOutOfBoundsException: -1 при запуске тестовых случаев.

Код для алгоритма приведен ниже. Для контекста я беру первый элемент в качестве сводного и заменяю его последним элементом для сравнения.

public static void quicksort(MyList<Integer> aList) {

        qSort(aList, 0, aList.size() - 1);

    }

    private static void qSort(MyList<Integer> list, int low, int high) {

        int pivot = partition(list, high, low);

        qSort(list, low, pivot - 1);
        qSort(list, pivot + 1, high);
    }

    private static int partition(MyList<Integer> list, int low, int high) {

        int pivot = list.get(low);

//swap first and last to move pivot to the end
        int temp = list.get(high);
        list.set(high, list.get(low));
        list.set(low, temp);

        int compare = list.get(low);
        int store = list.get(low);

        int pivotIndex = high;
        int compareIndex = low;
        int storeIndex = low;

        while (compare != list.get(high)) {

            if (compare < pivot) {

//swap compare and store
                int tempCompare = list.get(compareIndex);
                list.set(compareIndex, list.get(storeIndex));
                list.set(storeIndex, tempCompare);

                compareIndex++;
            }

            storeIndex++;
        }

//swap pivot and store
        int tempPivot = list.get(pivotIndex);
        list.set(pivotIndex, list.get(storeIndex));
        list.set(storeIndex, tempPivot);

        pivotIndex = storeIndex;
        return pivotIndex;
    }

Заранее спасибо.

...