public class QuickSort extends SortingAlgorithm implements Runnable {

    public static Integer[] array;
    
    public QuickSort(Integer[] toBeSorted, VisualizerFrame frame) {
        super(toBeSorted, frame);
        array = toBeSorted;
    }

    public void sort() {
        quickSort(0, getSize() - 1);
    }

    private void quickSort(int low, int high) {
        if (low < high) {
            int pivotIndex = partition(low, high);

            quickSort(low, pivotIndex - 1);
            quickSort(pivotIndex + 1, high);
        }
    }

    private int partition(int low, int high) {
        int pivot = array[high];
        int i = low - 1;

        for (int j = low; j < high; j++) {
            if (compare(j, high) <= 0) {
                i++;
                swap(i, j);
            }
        }

        swap(i + 1, high);
        return i + 1;
    }
}