Skip to content

Commit

Permalink
Merge pull request #3 from Jain-Saksham/Jain-Saksham-patch-3
Browse files Browse the repository at this point in the history
Quick Sort
  • Loading branch information
Jain-Saksham authored Oct 25, 2021
2 parents 1e88851 + 151f393 commit 096d5b2
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Quick Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;
void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
int partition(int arr[],int l,int r)
{
int pivot=arr[r];
int i=l-1;
for(int j=l;j<r;j++)
{
if(arr[j]<pivot)
{
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,r);
return i+1;
}
void quickSort(int arr[],int l,int r)
{
if(l<r)
{
int pi=partition(arr,l,r);

quickSort(arr,l,pi-1);
quickSort(arr,pi+1,r);
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quickSort(arr,0,n-1);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}

0 comments on commit 096d5b2

Please sign in to comment.