Skip to content

Commit

Permalink
Create Quick Sort in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushiChakrabarty authored Sep 2, 2020
1 parent 63a4afe commit af89662
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Quick Sort in C++
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//QuickSort in C++
#include<iostream>
using namespace std;


int Partition(int *A, int start, int end)
{
int i;
int pivot=A[end];
int part_i=start;
for(i=start; i<end; i++)
{
if(A[i]<=pivot)
{
swap(A[i],A[part_i]);
part_i++;
}
}
swap(A[part_i],A[end]);
return part_i;
}

void quicksort(int *A, int start, int end)
{
if(start<end)
{
int part_i=Partition(A,start,end);
quicksort(A,start,part_i-1);
quicksort(A,part_i+1,end);
}
}

int main()
{
int arr[20],i,n;
cout<<"Enter the number of elements that your array will contain:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
cin>>arr[i];

quicksort(arr,0,n-1);
for(i=0;i<n;i++)
cout<<arr[i]<<" ";

return 0;
}

0 comments on commit af89662

Please sign in to comment.