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

quick implemented #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions quicksort.cpp
@@ -0,0 +1,62 @@
#include<bits/stdc++.h>
using namespace std;
int partition(int *a,int start,int end)
{
int pivot=a[end];
//pivot value index

int P_index=start;
int i,t; //t is temporary variable

//Here we will check if array value is
//less than pivot
//then we will place it at left side
//by swapping

for(i=start;i<end;i++)
{
if(a[i]<=pivot)
{
t=a[i];
a[i]=a[P_index];
a[P_index]=t;
P_index++;
}
}
//Now exchanging value of
//pivot and P-index
t=a[end];
a[end]=a[P_index];
a[P_index]=t;

//at last returning the pivot value index
return P_index;
}
void Quicksort(int *a,int start,int end)
{
if(start<end)
{
int P_index=partition(a,start,end);
Quicksort(a,start,P_index-1);
Quicksort(a,P_index+1,end);
}
}
int main()
{
int n;
cout<<"Enter number of elements: ";
cin>>n;
int a[n];
cout<<"Enter the array elements:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
Quicksort(a,0,n-1);
cout<<"After Quick Sort the array is:\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}