PROGRAM OF QUICKSORT USING C++ Gain Infiniti

PROGRAM OF QUICKSORT USING C++


#include <iostream>
using namespace std;
//#define SIZE 10
class QuickSort
{
    public:
    int *array;
    void sort(int*,int,int);
    int partition(int*,int,int);
    void initial();
    void display();
    void sort_start();
    QuickSort()
    {
        array=new int[SIZE];
    }
    ~QuickSort()
    {
        delete[] array;
    }
};
// for inputting the elements;
void QuickSort::initial()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"Enter the "<<i<<" th index = ";
        cin>>array[i];
    }
}
void QuickSort::display()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"The "<<i<<" th index = "<<array[i]<<"\n";
    }
}
int QuickSort::partition(int* ar,int p,int r)
{
    int x=ar[r];
    int i=p-1;
    for(int j=p;j<r;j++)
    {
        if(ar[j] <= x)
        {
        i++;
            int t=ar[j];

            ar[j]=ar[i];
            ar[i]=t;
        }
       
    }
    int t=ar[i+1];
    ar[i+1]=ar[r];
    ar[r]=t;
    return (i+1);
}
void QuickSort::sort(int* ar,int p,int r)
{
    int q;
    if(p<r)
    {
        q=partition(ar,p,r);
        sort(ar,p,q-1);
        sort(ar,q+1,r);
    }
}
void QuickSort::sort_start()
{
    sort(array,0,SIZE-1);
}

 main()
{
    QuickSort object;
    object.initial();
    object.sort_start();
    object.display();
}


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 

Design By Manish and Ranjan