#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class QuickSort{
    public:
        int no_of_elements;
        int elements[10];
    public:
        void getarray();
        void sortit(int [], int, int);
    void partition(int [],int,int, int&);
    void display();
};
void QuickSort::getarray()
{
    cout<<"How many elements?: ";
    cin>>no_of_elements;
    cout<<"Insert array of element to sort: ";
    for(int i=0;i<no_of_elements;i++)
    {
    cin>>elements[i];
    }

}

void QuickSort::sortit(int x[], int lb, int ub)


{


    int j;
    if(lb >= ub)
     return;
    display();
    partition(x,lb,ub,j);
    sortit(x,lb,j-1);
    sortit(x,j+1,ub);

 }

void QuickSort::partition(int data[],int too_big_index,int too_small_index, int &pj)
{
     int j, pivot, temp, temp1;
     pivot=data[too_big_index];
     while(too_small_index > too_big_index)
     {
       while(data[too_big_index] <= pivot)
         ++too_big_index;
       while(data[too_small_index] >pivot)
         --too_small_index;
       if(too_big_index < too_small_index)
       {
        temp = data[too_big_index];
        data[too_big_index] = data[too_small_index];
        data[too_small_index] = temp;
    }
      }
      temp1=data[too_small_index];
      data[too_small_index]=pivot;
      data[too_big_index]=temp1;
      pj=too_small_index;
      //data[too_big_index]=data[too_small_index];
      //data[too_small_index]=pivot;

}

void QuickSort::display(){
    for(int i = 0 ; i < no_of_elements; i++){
    cout<<elements[i]<<" ";
    }
    cout<<endl;
    getch();
}
void main()
{
   QuickSort QS;
   clrscr();
    QS.getarray();
    cout<<"Sorting is given in step by step showing each iteration"<<endl;
    QS.sortit(QS.elements,0,QS.no_of_elements-1);
    QS.display();
    getch();
 }


public:
    int no_of_elements;
    int elements[10];
public:
    void getarray();
    void sortit(int [], int, int);
void partition(int [],int,int, int&);
void display();

void QuickSort::getarray()

{


cout<<"How many elements?: ";
cin>>no_of_elements;
cout<<"Insert array of element to sort: ";
for(int i=0;i<no_of_elements;i++)
{
cin>>elements[i];
}


}

void QuickSort::sortit(int x[], int lb, int ub)

{


int j;
if(lb >= ub)
 return;
display();
partition(x,lb,ub,j);
sortit(x,lb,j-1);
sortit(x,j+1,ub);



}

void QuickSort::partition(int data[],int too_big_index,int too_small_index, int &pj)

{


 int j, pivot, temp, temp1;
 pivot=data[too_big_index];
 while(too_small_index > too_big_index)
 {
   while(data[too_big_index] <= pivot)
     ++too_big_index;
   while(data[too_small_index] >pivot)
     --too_small_index;
   if(too_big_index < too_small_index)
   {
    temp = data[too_big_index];
    data[too_big_index] = data[too_small_index];
    data[too_small_index] = temp;
}
  }
  temp1=data[too_small_index];
  data[too_small_index]=pivot;
  data[too_big_index]=temp1;
  pj=too_small_index;
  //data[too_big_index]=data[too_small_index];
  //data[too_small_index]=pivot;



}

void QuickSort::display()

{


for(int i = 0 ; i < no_of_elements; i++){
cout<<elements[i]<<" ";
}
cout<<endl;
getch();


}

void main()

{

QuickSort QS;
clrscr();
QS.getarray();
cout<<"Sorting is given in step by step showing each iteration"<<endl;
QS.sortit(QS.elements,0,QS.no_of_elements-1);
QS.display();
getch();

}
This article has been dead for over six months. Start a new discussion instead.