diff --git a/JAVA/Quick_Sort.java b/JAVA/Quick_Sort.java new file mode 100644 index 0000000..8dee288 --- /dev/null +++ b/JAVA/Quick_Sort.java @@ -0,0 +1,47 @@ +public class QuickSort { + public static void quickSort(int[] array, int low, int high) { + if (low < high) { + int pivotIndex = partition(array, low, high); + + quickSort(array, low, pivotIndex - 1); + quickSort(array, pivotIndex + 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 main(String[] args) { + int[] arr = {12, 11, 13, 5, 6, 7}; + System.out.println("Original array:"); + printArray(arr); + + quickSort(arr, 0, arr.length - 1); + System.out.println("Array after Quick Sort:"); + printArray(arr); + } + + public static void printArray(int[] array) { + for (int i : array) { + System.out.print(i + " "); + } + System.out.println(); + } +}