import java.util.Scanner; public class QuickSort { // ---------- Partition Function ---------- public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; // last element as pivot int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; // Swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // Swap arr[i+1] and arr[high] (pivot) int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } // ---------- Quick Sort Function ---------- public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); // Left side quickSort(arr, pi + 1, high); // Right side } } // ---------- Main Method ---------- public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter size of array: "); int n = sc.nextInt(); int[] arr = new int[n]; System.out.println("Enter " + n + " elements:"); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } quickSort(arr, 0, n - 1); System.out.println("Sorted Array:"); for (int x : arr) { System.out.print(x + " "); } sc.close(); } }