Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  quicksort
Language: C/C++
Code:
#include <iostream>
using namespace std;
class sort
{
     private:
          int size;
          int a[10];
     public:
          sort()
          {
               cout<<"enter size of array";
               cin>>size;
               cout<<"\n"<<"enter elements of array";
               for(int j=0;j<size;j++)
               {
                    cin>>a[j];
               }
               resort(0,size-1);
          }
          void display()
          {
               for(int i=0;i<size;i++)
               {
                    cout<<"\t"<<a[i];
               }
          }
          void resort(int left,int right)
          {
               if(right-left<=0)
                    return;
               else
               {
                    int pivot=a[left];
                    int partition=quicksort(left,right,pivot);
                    resort(left,partition-1);
                    resort(partition+1,right);
               }
                    
          }
          int quicksort(int left,int right,int pivot)
          {
               int leftpntr=left;
               int rightpntr=right;
               while(true)
               {
                    while(pivot>a[++leftpntr]);
                    while(pivot<a[--rightpntr]);
                    if(left>=right)
                         break;
                    else
                         swap(leftpntr,rightpntr);
               }
               swap(left,rightpntr);
               return (rightpntr);
               
          }
          void swap(int i,int j)
          {
               int temp=a[i];
               a[i]=a[j];
               a[j]=temp;
          }
          
};
int main() {
     sort s;
     s.display();
     return 0;
}          
Comments: