Quick Sort
//Quick Sort in c................
#include<iostream>
using namespace std;
int partition(int *A,int start,int end)
{
int pivot=A[end];
int pIndex=start;
for(int i=start;i<end;i++)
{
if(A[i]<=pivot)
{
swap(A[i],A[pIndex]);
pIndex++;
}
}
swap(A[pIndex],A[end]);
return pIndex;
}
void quickSort(int *A,int start,int end)
{
if(start<end)
{
int pindex=partition(A,start,end);
quickSort(A,start,pindex-1);
quickSort(A,pindex+1,end);
}
}
int main()
{
int A[]={7,2,1,4,8,5,3,4};
cout<<"Before Sorting :"<<endl;
for(int i=0;i<8;i++)
{
cout<<A[i]<<endl;
}
quickSort(A,0,8);
cout<<"After Sorting :"<<endl;
for(int i=0;i<8;i++)
{
cout<<A[i]<<endl;
}
return 0;
}
No comments