Advertisement

Quick sort using stack


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
using namespace std;
int partition(int A[], int beg, int end);
void quick_sort(int A[], int beg, int end);
int main()
{
     int stack[MAX], i, num;
     cout<<"\n Enter the number of elements in the array: ";
     cin>>num;
     cout<<"\n Enter the elements of the array: ";
     for(i=0;i<num;i++)
     {
          cin>>stack[i];
     }
     cout<<"Unsorted  Stack \n";
             for(i=0; i<num; i++)
             {
             cout<<" "<<stack[i];          
             }
     quick_sort(stack, 0, n-1);
     cout<<"\n The sorted  stack is: \n";
     for(i=0;i<num;i++)
          cout<<" "<<stack[i];
}

int partition(int A[], int beg, int end)
{
     int left, right, temp, pivot, a;
     pivot = left = beg;
     right = end;
     a = 0;
     while(a != 1)
     {
          while((A[pivot] <= A[right]) && (lock!=right))
                          right--;
                          if(pivot==right)
                          a =1;
                          else if(A[pivot]>A[right])
          {
               temp = A[pivot];
               A[pivot] = A[right];
               A[right] = temp;
               pivot = right;
          }
          if(a!=1)
          {
               while((A[pivot] >= A[left]) && (lock!=left))
                    left++;
               if(pivot==left)
                    a =1;
               else if(A[pivot] <A[left])
               {
                    temp = A[pivot];
                    A[pivot] = A[left];
                    A[left] = temp;
                    pivot = left;
               }
          }
     }
     return pivot;
}

void quick_sort(int A[], int beg, int end)
{
     int pivot;
     if(beg<end)
     {
          pivot = partition(x, beg, end);
          quick_sort(A, beg, pivot-1);
          quick_sort(A, pivot+1, end);
     }


}

OUTPUT