The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Tuesday, January 11, 2011

Quicksort

#include<stdio.h>
#include<conio.h>
#define max 50
struct ele
{
 int info;
}ar[max],stup[max],stlw[max],tmp;


int noe,i,beg,end,top,loc,left,right;


void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}


void quick(int &beg,int &end,int &loc)
{
 left = beg;
 right = end;
 loc = beg;
 while(loc != right || loc !=left)
 {
  while(ar[loc].info <= ar[right].info && right != loc)
    right -= 1;
  if(ar[loc].info > ar[right].info)
  {
   tmp = ar[loc];
   ar[loc] = ar[right];
   ar[right] = tmp;
   loc = right;
  }
  while(ar[loc].info >= ar[left].info && left != loc)
    left += 1;
  if(ar[loc].info < ar[left].info)
  {
   tmp = ar[loc];
   ar[loc] = ar[left];
   ar[left] = tmp;
   loc = left;
  }
 }
}


void qk_srt()
{
top = 0;
stlw[top].info = 0;   stup[top].info = noe-1;
while(top != -1)
{
 beg = stlw[top].info;
 end = stup[top].info;
 top = top-1;
 quick(beg,end,loc);
 if(beg < loc-1)
 {
 top = top+1;
 stlw[top].info = beg;   stup[top].info = loc-1;
 }
 if(loc+1 < end)
 {
 top = top+1;
 stlw[top].info = loc+1;   stup[top].info = end;
 }
}
 printf("\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}


void main()
{
clrscr();
inp_dat();
qk_srt();
getch();
}

No comments:

Post a Comment