Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create quicksort.cpp #402

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions quicksort/Python/quicksort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
int partition(int arr[],int p,int q){
int k=p;
for(int i=p+1;i<=q;i++){
if(arr[i]<arr[p]){
k++;
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
int temp=arr[k];
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
arr[k]=arr[i];
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
arr[i]=temp;
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
}
}
int temp=arr[p];
arr[p]=arr[k];
arr[k]=temp;
return k;
}
void quicksort(int arr[],int p,int q){
if(p<q){
int k=partition(arr,p,q);
quicksort(arr,p,k-1);
quicksort(arr,k+1,q);
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
}
quicksort(arr,0,n-1);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
}
cout<<endl;
return 0;
neelabhtiwari marked this conversation as resolved.
Show resolved Hide resolved
}