private int divide(int startIndex, int endIndex) {

        int optimalIndex = getOptimalIndex(startIndex, endIndex);

        int pivot = a[optimalIndex];
        swap(optimalIndex, endIndex);

        int marker = startIndex;

        for (int i = startIndex; i < endIndex; i++) {
            if (a[i] <= pivot) {
                swap(i, marker);
                marker++;
            }
        }

        swap(marker, endIndex);
        return marker;
    }