Preview:
public class QuickSort {

    // Function to perform quick sort
    public static void quickSort(int[] array, int low, int high) {
        if (low < high) {
            // Find the pivot element such that elements smaller than pivot are on the left
            // and elements greater than pivot are on the right
            int pivotIndex = partition(array, low, high);
            
            // Recursively sort the elements on the left of the pivot
            quickSort(array, low, pivotIndex - 1);
            
            // Recursively sort the elements on the right of the pivot
            quickSort(array, pivotIndex + 1, high);
        }
    }

    // Partition the array and return the pivot index
    public static int partition(int[] array, int low, int high) {
        int pivot = array[high]; // Choosing the pivot element
        int i = (low - 1);       // Index of the smaller element

        for (int j = low; j < high; j++) {
            // If the current element is smaller than or equal to the pivot
            if (array[j] <= pivot) {
                i++;
                // Swap array[i] and array[j]
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        // Swap array[i + 1] and array[high] (or pivot)
        int temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;

        return i + 1;
    }

    public static void main(String[] args) {
        int[] array = { 10, 7, 8, 9, 1, 5 };
        int n = array.length;

        quickSort(array, 0, n - 1);

        System.out.println("Sorted array:");
        for (int value : array) {
            System.out.print(value + " ");
        }
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter