From b101d49dc29649b76437153f07d874895fef78fb Mon Sep 17 00:00:00 2001 From: apurva0411 <44138819+apurva0411@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:46:54 +0530 Subject: [PATCH] QuickSort_127.java Quicksort algorithm by Apurva --- Intermediate/Quicksort_127.java | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Intermediate/Quicksort_127.java diff --git a/Intermediate/Quicksort_127.java b/Intermediate/Quicksort_127.java new file mode 100644 index 00000000..f3875816 --- /dev/null +++ b/Intermediate/Quicksort_127.java @@ -0,0 +1,76 @@ +class QuickSort +{ + /* This function considers last element as pivot, + places the pivot element at its correct + position in sorted array, and places all + smaller (smaller than pivot) to left of + pivot and all greater elements to right + of pivot */ + int partition(int arr[], int low, int high) + { + int pivot = arr[high]; + int i = (low-1); // index of smaller element + for (int j=low; j Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i