From dae8dcca89eb4f0e9f079faf14dcbaa3759cb8d1 Mon Sep 17 00:00:00 2001 From: AarushiKapoor <54901924+AarushiKapoor@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:16:12 +0530 Subject: [PATCH] Update quickSort.h --- quickSort.h | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/quickSort.h b/quickSort.h index 5eb651f..bf72354 100644 --- a/quickSort.h +++ b/quickSort.h @@ -6,27 +6,19 @@ using namespace std; // ------------------------------------------------------------------------- // Partition function -int partition(vector &v, int start, int end) { +int partition(vector &a, int start, int end) { + + int pivot = a[end]; // Take the last element as a pivot + int i = start - 1; - int pivot = start; // Take the first element as a pivot - int i = start + 1; - int j = end; - while (i <= j) { - - // If element at the left is bigger than the pivot and - // element at the right is smaller, swap elements - - if (v[i] > v[pivot] && v[j] < v[pivot]) { - swap(v[i], v[j]); - } - else if (v[i] <= v[pivot]) { i++; } - else if (v[j] >= v[pivot]) { j--; } + for(int j=start;j &v, int start, int end) { if (start < end) { int pivot = partition(v, start, end); - quickSort(v, start, pivot); + quickSort(v, start, pivot-1); quickSort(v, pivot + 1, end); } }