From e9b1e2542b3473d6cc341e2a629be145e56d9ba9 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Mon, 21 Aug 2017 11:20:04 -0700 Subject: [PATCH] Updated QuickSort.java Updated quicksort to its generic version and changed the filename according to convention --- Sorts/QuickSort.java | 93 +++++++++++++++++++++++++++++++++++++++ Sorts/Quicksort.java | 102 ------------------------------------------- 2 files changed, 93 insertions(+), 102 deletions(-) create mode 100644 Sorts/QuickSort.java delete mode 100644 Sorts/Quicksort.java diff --git a/Sorts/QuickSort.java b/Sorts/QuickSort.java new file mode 100644 index 000000000..ec05938fa --- /dev/null +++ b/Sorts/QuickSort.java @@ -0,0 +1,93 @@ +/** + * + * @author Varun Upadhyay (https://github.com/varunu28) + * + */ + +class QuickSort { + + /** + * This method implements the Generic Quick Sort + * + * @param array The array to be sorted + * @param start The first index of an array + * @param end The last index of an array + * Sorts the array in increasing order + **/ + + public static > void QS(T array[], int start, int end) { + if (start < end) { + int PIndex = partition(array, start, end); + QS(array, start, PIndex - 1); + QS(array, PIndex + 1, end); + } + } + + /** + * This method finds the partition index for an array + * + * @param array The array to be sorted + * @param start The first index of an array + * @param end The last index of an array + * Finds the partition index of an array + **/ + + public static > int partition(T array[], int start, int end) { + T pivot = array[end]; + int PIndex = start; + for (int i=start;i> void swap(T[] array, int initial, int fin) { + T temp = array[initial]; + array[initial] = array[fin]; + array[fin] = temp; + } + + // Driver Program + public static void main(String[] args) { + + // For integer input + int[] arr = {3,4,1,32,0,2,44,111,5}; + Integer[] array = new Integer[arr.length]; + for (int i=0;i 0 1 2 3 4 5 32 44 111 + for (int i=0;i a b c d e + for(int i=0; i= 1){ - int pivot = ar[end]; - while (i< j){ - while (ar[i]=pivot && j>start){ - j--; - } - if (i