From 066cf1509793e881fd04f82665012303c58c9bca Mon Sep 17 00:00:00 2001 From: Shrey Gupta <75122294+Shreygupta2002@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:41:29 +0530 Subject: [PATCH] Added Quick Sort Algorithm Please add the hacktoberfest tag. --- c++/quick_sort.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 c++/quick_sort.cpp diff --git a/c++/quick_sort.cpp b/c++/quick_sort.cpp new file mode 100644 index 0000000..2d87bac --- /dev/null +++ b/c++/quick_sort.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; +typedef long long int ll; +void swap(int*a,int*b) +{ + int t=*a; + *a=*b; + *b=t; +} +int partition(int a[], int l, int r) +{ + // Taking the last element as the pivot + int pivot=a[r]; + + int pos=l-1; + for(int i=l;i>n; + int a[n]; + for(int i=0;i>a[i]; + } + + + // Creating a stack + int stack[n]; + int top=-1; + + stack[++top]=0; + stack[++top]=n-1; + + // While stack is not empty + while(top>=0) + { + int r=stack[top--]; + int l=stack[top--]; + + // Setting pivot element at its correct position in the sorted array + int temp=partition(a,l,r); + + // Taking the elements on the left in the stack + if(temp-1>l) + { + stack[++top]=l; + stack[++top]=temp-1; + } + + // Taking the elements on the right in the stack + if(temp+1(stop-start); + cerr<