Thursday, April 18, 2013

Radix Sort Sorting Technique and C Program


Radix Sort
Consider an array  A= { 54, 33, 25, 17, 09, 37, 13 }.

In radix sort traverse array from left to right. Check maximum no. of digits in the array.  In the above case, it is 2 so it will have 2-pass i.e, in the second pass information will be in sorted.
Traverse array from left to right,  check unit place of  each number in the array.  Unit place of 54 is 4, so put 54 in bucket 4.  Similarly, unit place of 33 is 3, so put 33 in buctet 3.  Similarly, check all the numbers in the array and put those numbers in their respective buckets

Bucket
 Pass-1
Pass-2
0


1


2


3
33,13

4
54

5
25

6


7
17,37

8


9
09


Now  Merge the Output of pass-1.  Result is as follows A= {33,13,54, 25, 17, 37, 09}

Now tranverse output of Pass-1 from left to right.  Now check 10th place of each number in the array.  10th place of 33 is 3, put 33 in bucket 3.  Similarly, 10th place of 13 is 1, put 13 in bucket 1.  Similarly, check all the numbers in the array and put those numbers in their respective buckets.


Bucket
 Pass-1
Pass-2
0

09
1

13,17
2

25
3
33,13
33,37
4
54

5
25
54
6


7
17,37

8


9
9


Now Merge the output of pass-2.  Result is as follows A= {09, 13, 17, 25, 33, 37, 54}

Note: if Maximum no. of digits is 3 then there will be 3 passes. If 4-digits, 4 passes etc.


The same can be done using the following calculation.

Consider an array  A= { 54, 33, 25, 17, 09, 37, 13 }.

For Pass-1 Calculation (unit place)
 
     54/100  =  54/1 =  54 =  54%10 =  4       Put 54 in bucket 4
     33/100  =  33/1 =  33 =  33%10 =  3       Put 33 in bucket 3
       25/100  =  25/1 =  25 =  25%10 =  5       Put 25 in bucket 5
     17/100  =  17/1 =  17 =  17%10 =  7       Put 17 in bucket 7
     09/100  =  09/1 =  09 =  09%10 =  9       Put 09 in bucket 9
     37/100  =  37/1 =  37 =  37%10 =  7       Put 37 in bucket 7
     13/100  =  13/1 =  13 =  13%10 =  3       Put 13 in bucket 3
Bucket
 Pass-1
Pass-2
0


1


2


3
33,13

4
54

5
25

6


7
17,37

8


9
09


Merge output of pass-1 i.e., A= {33,13,54, 25, 17, 37, 09}

For Pass-2 Calculation (10th  place).
 
     33/101  =  33/10 =  3 =  3%10 =  3       Put 33 in bucket 3
     13/101  =  13/10 =  1 =  1%10 =  1       Put 13 in bucket 1
       54/101  =  54/10 =  5 =  5%10 =  5       Put 54 in bucket 5
     25/101  =  25/10 =  2 =  2%10 =  2       Put 25 in bucket 2
     17/101  =  17/10 =  1 =  1%10 =  1       Put 17 in bucket 1
     37/101  =  37/10 =  3 =  3%10 =  3       Put 37 in bucket 3
     09/101  =  09/10 =  0 =  0%10 =  0       Put 09 in bucket 0
Bucket
 Pass-1
Pass-2
0

09
1

13,17
2

25
3
33,13
33,37
4
54

5
25
54
6


7
17,37

8


9
09


Now Merge the output of pass-2.  Result is as follows A= {09, 13, 17, 25, 33, 37, 54}



C Program for radix sort.

#include<stdio.h>
#include<math.h>
struct node
{
   int data;
   struct node *link;
};
struct node * createnodes(int n)
{
  int i;
  struct node *p,*first,*temp;
  printf("Enter elements\n");
  for (i=0; i<n; i++)
  {
    p= (struct node *) malloc(sizeof(struct node));
    scanf("%d", &p->data);
    p->link = NULL;

    if (i==0)
    {
      first=p;
    }
    else
    {
      temp=first;
      while (temp->link!=NULL)
      {
            temp=temp->link;
      }
      temp->link=p;
    }
  }
  return first;
}

void displaynodes(struct node *f)
{
  struct node *temp;
  temp=f;
  while (temp!=NULL)
  {
    printf("%d-->",temp->data);
    temp=temp->link;
  }
  printf("Null\n");
}


int MaxDigits(struct node *f)
{
  struct node *temp;
  int max,ct,n;
  temp=f;
  max=0;
  while (temp!=NULL)
  {
    n=temp->data;
    ct=0;
    while(n>0)
    {
      ct++;
      n=n/10;
    }
    if (ct>max)
    { max=ct; }
    temp=temp->link;
  }
  return max;
}

void RadixSort(struct node *f1)
{
  struct node *first,*f[10],*r[10],*temp;
  int max,i,j,k,p;
  max=MaxDigits(f1);
  first=f1;
  for(i=0;i<max;i++)
  {
    for(j=0; j<10;j++)
    {
      f[j]=NULL;
      r[j]=NULL;
    }
    while(first!=NULL)
    {
      temp=first;
      first=first->link;
      p=(temp->data/pow(10.0,(float)i));
      p=p%10;
      if(f[p]==NULL && r[p]==NULL)
      {
            f[p]=temp;
            r[p]=temp;
            temp->link=NULL;
      }
      else
      {
            r[p]->link=temp;
            temp->link=NULL;
            r[p]=temp;
      }
    }
    first=f[0];
    for(k=1;k<10;k++)
    {
      if (first==NULL && f[k]==NULL)
            first=NULL;
      else if (first==NULL && f[k]!=NULL)
            first=f[k];
      else
      {
            temp=first;
            while(temp->link!=NULL)
             { temp=temp->link; }
            temp->link=f[k];
      }
    }
    printf("Result of Pass - %d  : ",i+1);
    displaynodes(first);
  }
}




void main()
{
  int n;
  struct node *first;
  clrscr();
  printf ("Enter size of array/Linked list \n");
  scanf("%d",&n);
  first = createnodes(n);
  printf("Printing the elements from LL before sorting\n");
  displaynodes(first);
  RadixSort(first);
  getch();
}

Monday, March 18, 2013

C Program to create Tree using Menus


C Program to create Tree using Menus

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
struct node
{
   struct node *lchild;
   char data;
   struct node *rchild;
};
struct node *s[MAX];

int top,top1,st[MAX];

void push1(int p)
{
   if (top1==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top1++;
      st[top1]=p;
   }
}

int pop1()
{
   int p;
   if (top1<0)
     printf("Stack is Empty\n");
   else
   {
     p=st[top1];
     top1--;
   }
   return p;
}

void push(struct node *p)
{
   if (top==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top++;
      s[top]=p;
   }
}

struct node * pop()
{
  struct node *p;
   if (top<0)
     printf("Stack is Empty\n");
   else
   {
     p=s[top];
     top--;
   }
   return p;
}
void displaytree(struct node *root)
{
   int x,y,h,k;
   struct node *p;
   x=40;y=5;top1=0;h=0;top=0;
   p=root;
   s[top]=NULL;
   while(p!=NULL)
   {
     gotoxy(x,y);
     printf("%c", p->data);
     if (p->rchild!=NULL)
     {
       push(p->rchild);
       k=x+ceil(40.0/exp((h+1)*log(2)));
       push1(k);
       k=y+3;
       push1(k);
       push1(h+1);
     }
     if (p->lchild!=NULL)
     {
       p=p->lchild;
       h++;
       x=x-ceil(40.0/exp((h)*log(2)));
       y=y+3;
       gotoxy(x,y);
     }
     else
     {
       p=pop();
       if (top1!=0)
       {
             h=pop1();
             y=pop1();
             x=pop1();
             gotoxy(x,y);
       }
     }
   }

}
struct node * getnode()
{
   struct node *p;
   p= (struct node *) malloc(sizeof(struct node));
   printf("Enter data into a new node\n");
   fflush(stdin);
   scanf("%c",&p->data);
   p->lchild=NULL;
   p->rchild=NULL;
   return p;
}
struct node * createtree()
{
   struct node *root,*p,*t;
   char ch=0;
   top=0;
   s[top]=NULL;
   root=getnode();
   p=root;
   while(p!=NULL)
   {
      printf("Is the right child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') ||(ch=='y'))
      {
            t=getnode();
            p->rchild=t;
            push(t);
      }
      printf("Is the left child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') || (ch=='y'))
      {
            t=getnode();
            p->lchild=t;
            p=t;
      }
      else
      {
            p=pop();
      }
   }
   return root;
}

void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}
void rinorder(struct node *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}
void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}

void ipreorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p!=NULL)
   {
      if(p->rchild!=NULL)
       push(p->rchild);
      printf("%c ", p->data);
      if (p->lchild!=NULL)
       p=p->lchild;
      else
       p=pop();
   }
}
void iinorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     printf("%c ", p->data);
     if (p->rchild!=NULL)
     {
       p=p->rchild;
       while(p->lchild!=NULL)
       {
             push(p);
             p=p->lchild;
       }
     }
     else
     {
       p=pop();
     }
    }
}
void ipostorder(struct node *root)
{
   struct node *p,*temp;
   top=0;
   s[top]=NULL;
   p=root;
   temp=NULL;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     if ((p->rchild==NULL)||(p->rchild==temp))
     {
       printf("%c ", p->data);
       temp=p;
       p=pop();
     }
     else
     {
            push(p);
            p=p->rchild;
            while(p->lchild!=NULL)
            {
              push(p);
              p=p->lchild;
            }
     }
   }
}

void menudisplay(int n)
{
      int i,row=10,col=30;
      char opt[9][20]={"Create Tree","Display Tree","Recursive Preorder",
                               "Recursive Inorder","Recursive Postorder",
                               "Iterative Preorder","Iterative Inorder",
                               "Iterative Postorder","Exit"};
      clrscr();
      textattr(177);
      gotoxy(23,8);
      cprintf("TREES Demonstration\n");

      for (i=0;i<9;i++)
      {
            if (i==n)
            {
             textattr(66);
             gotoxy(col,row);
             cprintf("%s\n",opt[i]);
             row++;
            }
            else
            {
             textattr(7);
             gotoxy(col,row);
             cprintf("%s\n",opt[i]);
             row++;
            }
      }
}
int  selectoption()
{
      char ch=0;
      int curr=0;
      menudisplay(curr);

      while(ch!=13)
      {
            ch=getch();
            textattr(7);
            if (ch==27) return 27;
            if (ch==0) ch=getch();
            if (ch==72)
            {
              curr--;
              if (curr<0) { curr=8; }
              menudisplay(curr);
            }
            if (ch==80)
            {
              curr++;
              if (curr==9) { curr=0; }
              menudisplay(curr);
            }
      }
      return curr;
}

void main()
{
   int opt;
   struct node *root;
   clrscr();
   while (1)
   {
     opt=selectoption();
     if ((opt==27) ||( opt==8)) { break; }
     clrscr();
     switch(opt)
     {
            case 0:{ root= createtree();                  break;   }
            case 1:{ displaytree(root);                   break;   }
            case 2:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         rpreorder(root);
                         break;
                   }
            case 3:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         rinorder(root);
                         break;
                   }
            case 4:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         rpostorder(root);
                         break;
                   }
            case 5:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         ipreorder(root);
                         break;
                   }
            case 6:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         iinorder(root);
                         break;
                   }
            case 7:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         ipostorder(root);
                         break;
                   }
            case 8:{ exit(0);      }
     }
     gotoxy(30,22);
     printf("Press any Key........\n");
     getch();
   }
  textattr(177);
  gotoxy(20,20);
  cprintf(" Tree Demonstration is Over.   Press any key....");
  getch();
}

C Program to Create Tree, Display Tree, Iterative Preorder, Inorder and Postorder


C Program to Create Tree, Display Tree, Iterative Preorder, Inorder and Postorder

 #include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
struct node
{
   struct node *lchild;
   char data;
   struct node *rchild;
};
struct node *s[MAX];

int top,top1,st[MAX];

void push1(int p)
{
   if (top1==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top1++;
      st[top1]=p;
   }
}

int pop1()
{
   int p;
   if (top1<0)
     printf("Stack is Empty\n");
   else
   {
     p=st[top1];
     top1--;
   }
   return p;
}

void push(struct node *p)
{
   if (top==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top++;
      s[top]=p;
   }
}
struct node * pop()
{
  struct node *p;
   if (top<0)
     printf("Stack is Empty\n");
   else
   {
     p=s[top];
     top--;
   }
   return p;
}


void displaytree(struct node *root)
{
   int x,y,h,k;
   struct node *p;
   x=40;y=5;top1=0;h=0;top=0;
   p=root;
   s[top]=NULL;
   while(p!=NULL)
   {
     gotoxy(x,y);
     printf("%c", p->data);
     if (p->rchild!=NULL)
     {
       push(p->rchild);
       k=x+ceil(40.0/exp((h+1)*log(2)));
       push1(k);
       k=y+3;
       push1(k);
       push1(h+1);
     }
     if (p->lchild!=NULL)
     {
       p=p->lchild;
       h++;
       x=x-ceil(40.0/exp((h)*log(2)));
       y=y+3;
       gotoxy(x,y);
     }
     else
     {
       p=pop();
       if (top1!=0)
       {
             h=pop1();
             y=pop1();
             x=pop1();
             gotoxy(x,y);
       }
     }
   }

}


struct node * getnode()
{
   struct node *p;
   p= (struct node *) malloc(sizeof(struct node));
   printf("Enter data into a new node\n");
   fflush(stdin);
   scanf("%c",&p->data);
   p->lchild=NULL;
   p->rchild=NULL;
   return p;
}


struct node * createtree()
{
   struct node *root,*p,*t;
   char ch=0;
   top=0;
   s[top]=NULL;
   root=getnode();
   p=root;
   while(p!=NULL)
   {
      printf("Is the right child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') ||(ch=='y'))
      {
            t=getnode();
            p->rchild=t;
            push(t);
      }
      printf("Is the left child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') || (ch=='y'))
      {
            t=getnode();
            p->lchild=t;
            p=t;
      }
      else
      {
            p=pop();
      }
   }
   return root;
}

void ipreorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p!=NULL)
   {
      if(p->rchild!=NULL)
       push(p->rchild);
      printf("%c ", p->data);
      if (p->lchild!=NULL)
       p=p->lchild;
      else
       p=pop();
   }
}


void iinorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     printf("%c ", p->data);
     if (p->rchild!=NULL)
     {
       p=p->rchild;
       while(p->lchild!=NULL)
       {
             push(p);
             p=p->lchild;
       }
     }
     else
     {
       p=pop();
     }
    }
}


void ipostorder(struct node *root)
{
   struct node *p,*temp;
   top=0;
   s[top]=NULL;
   p=root;
   temp=NULL;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     if ((p->rchild==NULL)||(p->rchild==temp))
     {
       printf("%c ", p->data);
       temp=p;
       p=pop();
     }
     else
     {
            push(p);
            p=p->rchild;
            while(p->lchild!=NULL)
            {
              push(p);
              p=p->lchild;
            }
     }
   }
}

  


void main()
{
   int opt;
   struct node *root;
   clrscr();
   while (1)
   {
     clrscr();
     printf("Demonstration of Tree Traversal using Iterative method\n");
     printf("1. Create Tree\n");
     printf("2. Display Tree\n");
     printf("3. Preorder\n");
     printf("4. Inorder\n");
     printf("5. Postorder\n");
     printf("6. Exit\n");
     printf("Enter Choice: ");
     scanf("%d",&opt);
     switch(opt)
     {
            case 1:{ root= createtree();    break;   }
            case 2:{ clrscr(); displaytree(root);     break;   }
            case 3:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         ipreorder(root);
                         break;
                   }
            case 4:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         iinorder(root);
                         break;
                   }
            case 5:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         ipostorder(root);
                         break;
                   }
            case 6:{ exit(0);      }
     }

     gotoxy(30,22);
     printf("Press any Key........\n");
     getch();
   }
}