From d1aa6319de73d8577687a6fb412b240acc1a8e35 Mon Sep 17 00:00:00 2001 From: pg8469 <43347426+pg8469@users.noreply.github.com> Date: Thu, 31 Oct 2019 12:25:07 +0530 Subject: [PATCH] Create 3_way_quicksort.cpp Better quicksort which works on duplicate data also. --- Sorting/QuickSort/C++/3_way_quicksort.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Sorting/QuickSort/C++/3_way_quicksort.cpp diff --git a/Sorting/QuickSort/C++/3_way_quicksort.cpp b/Sorting/QuickSort/C++/3_way_quicksort.cpp new file mode 100644 index 00000000..8dc4a638 --- /dev/null +++ b/Sorting/QuickSort/C++/3_way_quicksort.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +void swap(int* arr,int a,int b){ + int temp=arr[a]; + arr[a]=arr[b]; + arr[b]=temp; +} +void qs(int* arr,int start,int end){ + if(start>=end) + return; + int pivot=arr[start]; + int first=start,last=end,mid=start+1; + while(mid<=last){ + if(arr[mid]pivot) swap(arr,mid,last--); + else mid++; + } + qs(arr,start,first-1); + qs(arr,last+1,end); +}