forked from hanshulll/CodeCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Jain-Saksham/Jain-Saksham-patch-3
Quick Sort
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |