HI I AM TANZIM NOW I STUDYING IN PUNDRA UNIVERSITY,BOGRA,BANGLADESH ***WELCOME MY WEBSITE**.

Quick Sort

#include<iostream>
#include<cstdlib>
using namespace std;
#define max 1000

long Partition(long a[], long p, long r);
void QuickSort(long a[], int p, int r);


int main()
{
long a[max],i,j,n;

cout<<"How many numbers.."<<endl;
cin>>n;
cout<<"Now enter the numbers"<<endl<<endl;
for(i=0;i<n;i++)
cin>>a[i];

QuickSort(a,0,n-1);

cout<<"After sorting the elements are..."<<endl<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<"  ";
cout<<endl<<endl;
system("PAUSE");
}


void QuickSort(long a[], int p, int r)
{
    int q;
    if(p<r)
    {
        q=Partition(a,p,r);
        QuickSort(a,p,q-1);
        QuickSort(a,q+1,r);
    }
}



long Partition(long a[], long p, long r)
{
    long i,j,x,temp;
    x=a[r];
    i=p-1;
    for(j=p;j<=(r-1);j++)
    {
        if(a[j]<=x)
        {
            i++;
            temp=a[j];
            a[j]=a[i];
            a[i]=temp;
        }
    }
    temp=a[i+1];
    a[i+1]=a[r];
    a[r]=temp;

    return (i+1);
}


Output

No comments:

Post a Comment