Simple Quick Sort Program in java

Arrange the given inputs in ascending order using the Quick sort algorithms.

public class Quick {
    public static void main(String[] args) {
        int[] array = {8, 6, 2, 3, 5, 1, 0};
        quickSort(array, 0, array.length - 1);
        printArray(array);
    }

    public static void quickSort(int[] array, int low, int high){
        if (low < high){
            int pi = partition(array, low, high);
            quickSort(array, low, pi - 1);
            quickSort(array, pi + 1, high);
        }
    }

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

        for (int j = low; j < high; j++){
            if (array[j] <= pivot){
                i++;
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        int temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;

        return i + 1;

    }
    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
}

INPUT:
8 6 2 3 5 1 0

OUTPUT:
0 1 2 3 5 6 8

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top