Skip to content
Open
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
47 changes: 47 additions & 0 deletions C++/Algorithms/quick sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
using namespace std;
void swap(int a[],int i,int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
int partition(int a[],int l,int r){
int i=l-1;

int pivot=a[r];
for(int j=l;j<r;j++){
if(a[j]<pivot){
i++;
swap(a,i,j);
}
}
swap(a,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 arr[100],n;
cout<<"Enter the number of elements in an array";
cin>>n;
cout<<"Enter the elements in an array"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quicksort(arr,0,n);
cout<<"the sorted array using quicksort is"<<endl;
for(int j=0;j<5;j++){
cout<<arr[j]<<" ";
}

return 0;
}