Friday, August 31, 2012

C Program for Quick Sort

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
int *a,key,l,r,n;
void quick(int ,int );
void main()
{
 clrscr();
 int i;
 cout<<"Enter size";
 cin>>n;
 a=(int*)malloc(n*sizeof(int));
 cout<<"\nEnter elements";
 for (i=0;i<n;i++)
 cin>>a[i];
 cout<<"\nAfter quick sorting\n";
 quick(0,n-1);
 for(i=0;i<n;i++)
 cout<<" "<<a[i];
 getch();
}
void quick(int lb,int ub)
{
 int t;
 key=a[lb];
 l=lb+1;
 r=ub;
 while(l<=r)
 {
  while(key>a[l])
  l++;
  while(key<a[r])
  r--;
  if(l<=r)
  {
   t=a[l];
   a[l]=a[r];
   a[r]=t;
  // l++;
   //r--;
  }
 }
 t=a[lb];
 a[lb]=a[r];
 a[r]=t;
 cout<<endl;
 if(r>lb)
 quick(lb,r-1);
 if(l<ub)
 quick(r+1,ub);

}

C Program for Radix Sort

/**************************************************************************
////////////////// -*-  Program on Radix Sort  -*- \\\\\\\\\\\\\\\\\\\\\\\\
//////////////// --*-- By.:Vikrantsingh.M.Bisen --*-- \\\\\\\\\\\\\\\\\\\\\
***************************************************************************/
#include<iostream.h>
#include<math.h>
#include<conio.h>
int bucket[10][10];
int top[10];
int array[10],temp[10];
int i,j,k,size,length;
void initialize()
{
    for(i=0;i<10;i++)
    top[i]=0;
    for(i=0;i<10;i++)
    for(int r=0;r<10;r++)
    bucket[i][r]=0;
}
void copy_to_bucket()
{
    for(i=0;i<size;i++)
    {
     int lsb=temp[i]%10;
     bucket[lsb][top[lsb]]=array[i];
     top[lsb]++;
    }
    //---------Printing
    cout<<"\nBucket Contaion.:\n  ";
    for(i=0;i<10;i++)
    {for(int h=0;h<10;h++){ cout<<" "<<bucket[i][h];}cout<<endl;}
    cout<<"\n TOP Contain.:\n ";
    for(i=0;i<10;i++)
    cout<<" "<<top[i];

}
void copy_to_array()
{
    int k=0;
    for(i=0;i<10;i++)
    {
     int j=0;
     while(j<top[i])
     {
      array[k]=bucket[i][j];
      k++;j++;
     }
    }
    cout<<endl<<"Array Contain.:\n";
    for(i=0;i<size;i++)
    cout<<" "<<array[i];
}
void find_max_length()
{
  int y=1;
  length=0;
  for(i=0;i<size;i++)
  {
   int x=array[i];
   y=0;
   while(x!=0)
   {y++;x=x/10;}
   if(y>length)length=y;
  }

}
void main()
{
  clrscr();
  cout<<"Enter Size";
  cin>>size;
  for(int i=0;i<size;i++)
   cin>>array[i];
//  -------------
  for(i=0;i<size;i++)
  temp[i]=array[i];
  //-------
  find_max_length();
  //--------
  int p=1;
  for(int m=0;m<length;m++)
  {
    initialize();
    //-------assign from array to bucket
    copy_to_bucket();
    //-------assign from bucket to array
    copy_to_array();
    //---
   getch();
    for(i=0;i<size;i++)
    temp[i]=array[i]/pow(10,p);p++;
  }
 cout<<"\nSorted Array.:";
 for(i=0;i<size;i++)
 cout<<"  "<<array[i];
 getch();
}

C Program for Selection Sort

/*Selection Sort*/
#include<iostream.h>
#include<conio.h>
class sort
{
  private:
  int *a,size,i;
  public:
  sort()
  {
   cout<<"Enter size";
   cin>>size;
   a=new int[size];
  }
  void read()
  {
   cout<<"\nEnter Elements\n";
   for(i=0;i<size;i++)
   {
    cin>>a[i];
   }
  }
  void print()
  {
   cout<<"\nSorted Elements\n";
   for(i=0;i<size;i++)
   {
    cout<<" "<<a[i];
   }
  }
  void sorting()
  {
   for(i=0;i<size;i++)
   {
     for(int j=0;j<size;j++)
     {
       if(a[i]<a[j])
       {
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
       }
     }
   }
  }
};
void main()
{
 sort a;
 a.read();
 a.sorting();
 a.print();
 getch();
}

C Program for Stack Push POP Operations

/*Push and Pop Operation on Stack*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
class stack
{
  char ch;
  int x[10],i,TOP;
  public:
  stack()
  { TOP=0;}
  void push(int);
  int pop();
  void process();
};
void stack::push(int y)
{
  if (TOP>=10)
  {
  cout<<"SORRY! OVERFLOW ERROR......";
  exit(0);
  }
 x[++TOP]=y;
}
int stack::pop()
{
  if (TOP<1)
  {
  cout<<"SORRY! UNDERFLOW ERROR......";
  exit(0);
  }
  return(x[TOP--]);
}
void stack::process()
{
 while(1)
 {
//   clrscr();
   cout<<"WHAT WOULD YOU LIKE TO DO?\n";
   cout<<"1.PUSH \t 2.POP \t3.EXIT";
   ch=getche();
   switch(ch)
   {
    case '1':
       cout<<"\nEnter value.:";
       cin>>i;
       push(i);
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
       break;
    case '2':
      i=pop();
      cout<<"\nVALUE POPED.:"<<i;
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
      break;
    case '3':
      exit(0);
    default:
      cout<<"SORRY! Invalid Input";
   }
   getch();
 }
}
void main()
{
  clrscr();
  stack s;
  s.process();
  getch();
}

C Program for Stack using linked list

/* STACK OPERATION USING LINK LIST */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
#include<process.h>
struct node
{
  int item;
  struct node *add;
}*h;
void push(struct node *a,int x)
{
  if(a==NULL)
  {
   a=(node *)malloc(sizeof(node));
   h=a;
   a->item=x;
   a->add=NULL;
   return;
  }
  while(a->add!=NULL)
  {
   a=a->add;
  }
  a->add=(node *)malloc(sizeof(node));
  if(a->add==NULL){cout<<"\n\aStack Overflow.";return;}
  a=a->add;
  a->item=x;
  a->add=NULL;
}
int pop(struct node * a)
{
 struct node *temp;
 if(a->add==NULL)h=NULL;
 if(a==NULL){cout<<"\n\aStack Underflow.";return 0;}
 while(a->add!=NULL)
  {
    temp=a;
    a=a->add;
  }
  temp->add=NULL;
  int x=a->item;
  a=NULL;
  return x;
}
void show(struct node *a)
{
   cout<<"List.:";
   while(a!=NULL)
   {
     cout<<" "<<a->item;
     a=a->add;
   }
}
void main()
{
  clrscr();
  cout<<"\nSTACK OPERATION USING LINK LIST\n";
  h=NULL;
  while(1)
  {
   cout<<"\n1.PUSH\t2.POP\t3.EXIT\n";
   char ch;
   int n;
   ch=getch();
   switch(ch)
   {
     case '1':
      cout<<"Enter no.:";
      cin>>n;
      push(h,n);
      show(h);
      break;
     case '2':
      cout<<"Poped.:"<<pop(h);
      show(h);
      break;
     case '3':
       exit(0);
   }
  }
}

C Program to calculate depth of Tree

/* Height/Depth of a Tree */
#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct tree
{
       int item,level;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"\nEnter value";
     cin>>p->item;
     cout<<"\nDo you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          p->left->level=p->level+1;
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          p->right->level=p->level+1;
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
static int max=0;
void findmax(tree *p)
{
  if(p==NULL)return;
  if(p->level>max)
  max=p->level;
  findmax(p->left);
  findmax(p->right);
}
int main()
{
     clrscr();
     struct tree *a;
     a=(tree*)malloc(sizeof(tree));
     a->level=0;
     cout<<"Enter Tree\n";
     create(a);
     findmax(a);
     cout<<"\nHeight of a Binary Tree is"<<max;
     getch();
     return 0;
}

c Program for Checking tree for mirror image

/* Mirror Image */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
       int item;
       struct tree *right,*left;
};
void create(struct tree *p)
{
     int choice;
     cout<<"Enter value";
     cin>>p->item;
     cout<<"Do you Want to Enter left Child of "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->left=(tree*)malloc(sizeof(tree));
          create(p->left);
     }
     else
     {
          p->left=NULL;
     }
     cout<<"Do you Want to Enter Right Child of  "<<p->item<<" .1.YES/2.NO";
     cin>>choice;
     if(choice==1)
     {
          p->right=(tree*)malloc(sizeof(tree));
          create(p->right);
     }
     else
     {
          p->right=NULL;
     }
}
void print_inorder(struct tree *p)
{
     if(p==NULL)return;
     print_inorder(p->left);
     cout<<endl<<p->item;
     print_inorder(p->right);
}
int compare(tree *p,tree *q)
{
   if(p==NULL&&q==NULL)
   return 1;
   else if(p==NULL||q==NULL)
   return 0;
   else if(p->item==q->item)
   {
    int s1=compare(p->left,q->right);
    int s2=compare(p->right,q->left);
    return s1*s2;
   }
   else
   return 0;
}
void main()
{
     clrscr();
     struct tree *a;
     cout<<"\nProgram To Compare Two Trees\n";
     cout<<"Enter First Tree\n";
     a=(tree*)malloc(sizeof(tree));
     create(a);
     struct tree *b;
     cout<<"\nEnter Second Tree\n";
     b=(tree*)malloc(sizeof(tree));
     create(b);
     if(compare(a,b)==1)
     cout<<"**Trees are MIRROR IMAGE of each other**";
     else
     cout<<"**Trees are not MIRROR IMAGE of each other**";
     getch();
}