Quick Sort (p):

<br />#include<iostream>
#include<conio.h>
using namespace std;

int partition(int a[],int beg,int end)
{
int loc=beg,temp;
while(1)
{
while(a[loc]<=a[end] && (loc!=end))
{
end=end-1;
}
if(loc==end)
return loc;
else if(a[loc]>a[end])
{
temp=a[loc];
a[loc]=a[end];
a[end]=temp;
loc=end;
}
while(a[loc]>=a[beg] && loc!=beg)
{
beg=beg+1;
}
if(loc==beg)
return loc;
else if(a[loc]<a[beg])
{
temp=a[loc];
a[loc]=a[beg];
a[beg]=temp;
loc=beg;
}
}
}

void quick_sort(int a[],int beg,int end)
{
int loc;
if(beg<end)
{
loc=partition(a,beg,end);
quick_sort(a,beg,loc-1);
quick_sort(a,loc+1,end);
}
}

int main()
{
int a[10],n;
cout<<"Enter the size of array:"<<endl;
cin>>n;
cout<<endl<<"Enter elements here:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quick_sort(a,0,n-1);
cout<<endl<<"After sorting array is:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\t\t@Myprogworld";
getch();
return 0;
}

corollary :-
QSC

Leave a comment