From 3b427f3162447d965d4a936b962a8da71c739ac0 Mon Sep 17 00:00:00 2001 From: Harshit_Dongre <95414266+Harshit1020@users.noreply.github.com> Date: Sun, 30 Oct 2022 21:22:07 +0530 Subject: [PATCH] Create Quick_Sort.java --- Quick_Sort.java | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Quick_Sort.java diff --git a/Quick_Sort.java b/Quick_Sort.java new file mode 100644 index 0000000..58ee232 --- /dev/null +++ b/Quick_Sort.java @@ -0,0 +1,62 @@ +//Java Program for Merge Sort +class Main { + + public static void display(int[] arr, int size) + { + for(int i = 0; i < size; i++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + } + + public static void main(String[] args) + { + int[] a = {12, 11, 13, 5, 6, 7 }; + + int size = a.length; + display(a, size); + + quickSort(a, 0, size - 1); + display(a, size); + } + + static void swap(int[] arr, int i, int j) + { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + static void quickSort(int[] arr, int low, int high) + { + if (low < high) + { + + int indexPI = partition(arr, low, high); + + quickSort(arr, low, indexPI - 1); + quickSort(arr, indexPI + 1, high); + } + } + + static int partition(int[] arr, int low, int high) + { + + int pivot = arr[high]; + int swapIndex = (low - 1); + + for (int j = low; j <= high- 1; j++) + { + + if (arr[j] < pivot) + { + swapIndex++; + swap(arr, swapIndex, j); + } + } + + swap(arr, swapIndex + 1, high); + + return (swapIndex + 1); + } +}