From 1b6a4d34b45550d42b39192615fa8f9d8e7406d3 Mon Sep 17 00:00:00 2001 From: Janit Sriganeshaelankovan Date: Thu, 14 Oct 2021 12:00:24 -0400 Subject: [PATCH] added Java QuickSort --- Sorting Algorithms/Java/QuickSort.java | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Sorting Algorithms/Java/QuickSort.java diff --git a/Sorting Algorithms/Java/QuickSort.java b/Sorting Algorithms/Java/QuickSort.java new file mode 100644 index 00000000..01ef2d40 --- /dev/null +++ b/Sorting Algorithms/Java/QuickSort.java @@ -0,0 +1,46 @@ +import java.util.Arrays; + +public class QuickSort { + public static void main(String[] args) { + int[] arr = {5,75,3,65,2,1,3,4,4,88}; + System.out.println(Arrays.toString(arr)); + quickSort(arr, 0, arr.length-1); + System.out.println(Arrays.toString(arr)); + } + + public static void quickSort(int[] arr, int leftStart, int rightEnd){ + if(leftStart < rightEnd){ + int partition = partition(arr, leftStart, rightEnd); + quickSort(arr, leftStart, partition - 1); + quickSort(arr, partition+1, rightEnd); + } + } + + public static int partition(int[] arr, int low, int high){ + + // improved performance when the array is already sorted + int mid = low + (high - low) / 2; + swap(arr, mid, high); + + // choosing the high as the pivot + int pivot = arr[high]; + int i = low - 1; + + for (int j = low; j < high; j++){ + if(arr[j] <= pivot){ + i++; + swap(arr, j, i); + } + } + + swap(arr, high, i+1); + + return i+1; + } + + public static void swap(int[] arr, int a, int b) { + int temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; + } +}