Saturday, 29 September 2012

TWO STACKS IN ONE ARRAY IN C CODE

#include<stdio.h>
#define MAX 10
int top1 = -1;
int top2 = MAX;
int a[MAX];
void push(int e,int s);
int pop(int s);

int main()
{
    int n ,e,s;
    do
    {
        printf("\n enter choice \n 1 for push \n 2 for pop \n 3 for exit \n  ");
        scanf("%d",&n);
        switch(n)
        {
            case 1:
            {
                printf("\n choose the stack 1 or 2 \n");
                scanf("%d",&s);
                printf("\n enter the element \n");
                scanf("%d",&e);
                push(e,s);
                break;
            }
            case 2:
            {
             int e;
             printf("\n enter your stack preffrence 1 or 2 ");
             scanf("%d",&s);
             e = pop(s);
             if(e != -1)
             {
                 printf("\n the pop element = %d \n",e);
             }
             break;
            }
            default:
            printf("\n enter the correct choice \n");
        }
    }while(n!=3);
}
int pop(int s)
{
    int temp;
    if(s==1)
    {
        if(top1==-1)
        {
            printf("stack 1 is empty \n");
            return -1;
        }
        else
        {
            temp = a[top1];
            top1--;
           return temp;
        }
   }
else
{
    if(top1==MAX)
        {
            printf("stack 2 is empty \n");
            return -1;
        }
        else
        {
            temp = a[top2];
            top2++;
           return temp;
        }
}
}
void push(int e,int s)
{
    if(top1==top2)
        {
            printf("\n the queue is full");
            return;
        }
    else
  {
    if(s==1)
    {
        top1++;
        a[top1]=e;
        printf("\n element inserted in stack 1 \n");
    }
  else
 {
     top2--;
     a[top2]==e;
     printf("\n element inserted into stack 2 \n ");

  }
    }

}

CIRCULAR QUEUE IN C USING ARRAY

#include<stdio.h>

#define MAX 10
int front=-1;
int rear = 0;
int a[MAX];
void enqueue(int e);
int dequeue();

int main()
{
    int n,m,e;
    do
{
  printf(" \n press a key \n 1 for enque \n 2 for dequeue \n 3 to exit \n ");
  scanf("%d",&n);
  switch(n)
  {

  case 1 :
  {
      printf("\n enter no to be entered");
      scanf("%d",&e);
      enqueue(e);
      break;
  }
  case 2:
  {
      int e;
      e = dequeue();
      if(e!= -1)
      {
      printf("the dequed no is = %d",e);
      }
      break;
  }
  default :
  printf("\n you made the wrong choice");
  }
}while(n!=3);

}
void enqueue(int e)
{

    if(front == rear+1)
    printf("\n the queue is full sorry ");

    else
    {
        a[rear]= e;
        rear = (rear+1)%MAX;
        if(front== -1)
         front++;
    }
}
int dequeue()
{
    int temp;
    if(front == rear)
    {
    printf("\n queue is empty");
    return -1;
    }
    else
    {
        temp = a[front];
        front = (front+1)%MAX;
        return temp;
    }
}

Friday, 28 September 2012

SHELL SORT IN C

#include<stdio.h>
#include<math.h>
void shell_sort(int n, int a[n]);
int n;
int main()
{
    int i , j;
    printf("\n enter no of elements \n ");
    scanf("%d",&n);
    int a[n];
    printf("\n enter elements");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    shell_sort(n,a);


    return 0;
}
void shell_sort(int n,int a[])
{
    int i,j,x,count,gap,temp;
    gap = ceil(n/2);
    while(gap)
    {
        for(i=gap;i<n;i++)
        {
            x = a[i];
            for(j=i-gap;j>=0 ; j=j-gap)
            {
                if(a[j]>x)
                {
                temp = a[j];
                a[j]= a[j+gap];
                a[j+gap]= temp;
                }
                else
                break;
            }
        }
        gap = ceil(gap/2);
    }
     for(i=0;i<n;i++)
    {
        printf("\n %d",a[i]);
    }
}

Sunday, 16 September 2012

NO OF BINARY SEARCH TREE POSSIBLE ON NODES IN C


/* following prog. takes no of elements as input and return no of possible BST on that number */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

 long int bst(long int a);
 long int BST[100];
 //array to store solutions of subproblems.assume that n should less the 100 here.
 //you can increase this value.
 long int n;

 int main()
{
int i;
long int total;
printf("\n enter no. of elements = \n");//take the no for which u want to calculate no of BST.
scanf("%ld",&n);
//initial set all subproblems sol. to 0.
 for (i=2;i<=n-1;i++)
 BST[i]=0;

BST[0]= 1;//set no of BST for nodes 1,0, equal 1.
BST[1] = 1;

//pass the value to function bst.
total = bst(n);
//total holds the final result

printf("\n the possible no of binary search tree on %ld elements is %ld",n,total);
return 0;
}

long int bst(long int a)
{
    int i;long int count;
//if no_of elements 0 or 1 return 1.
    if(a == 0 || a == 1 )
    return 1;
//if solution of subproblem is there return the value.
   else if(BST[a] != 0)
   return BST[a];

  else
 {
      count=0;
  for (i=1;i<=a;i++)
 {
     //choose 1 to n roots at one time then call for both subtrees left and right recursivley.
  count= count+ bst(i-1)*bst(a-i);
  printf(" \n the count is  on nodes is = %ld \n",count);
 }
 BST[a]=count;
 return count;


  }
}

Friday, 14 September 2012

Tuesday, 11 September 2012

hash collition

Calculating the Probability of a Hash Collision

There are many choices of hash function, and the creation of a good hash function is still an active area of research. Some hash functions are fast; others are slow. Some distribute hash values evenly across the available range; others don’t. If you’re interested in the real-world performance of a few known hash functions, Charles Bloom and strchr.com offer some comparisons. For our purposes, let’s assume the hash function is pretty good — it distributes hash values evenly across the available range.
In this case, generating hash values for a collection of inputs is a lot like generating a collection of random numbers. Our question, then, translates into the following:
Given k randomly generated values, where each value is a non-negative integer less than N, what is the probability that at least two of them are equal?
It turns out it’s actually a bit simpler to start with the reverse question: What is the probability that they are all unique? Whatever the answer to the reverse question, we can just subtract it from one, and we’ll have the answer to our original question.
Given a space of N possible hash values, suppose you’ve already picked a single value. After that, there are N-1 remaining values (out of a possible N) that are unique from the first. Therefore, the probability of randomly generating two integers that are unique from each other is \frac{N-1}{N}.
After that, there are N-2 remaining values (out of a possible N) that are unique from the first two, which means that the probability of randomly generating three integers that are all unique is \frac{N-1}{N}\times\frac{N-2}{N}. (We can multiply the probabilities together because each random number generation is an independent event.)
In general, the probability of randomly generating k integers that are all unique is:
\frac{N-1}{N}\times\frac{N-2}{N}\times\dots\times\frac{N-(k-2)}{N}\times\frac{N-(k-1)}{N}
On a computer, this can be quite slow to evaluate for large k. Luckily, the above expression is approximately equal to:
e^{\frac{-k(k-1)}{2N}}
which is a lot faster to compute. How do we know this is a good approximation? Well, it can be shown analytically, using the Taylor expansion of e^x and an epsilon-delta proof, that the approximation error tends to zero as N increases. Or, you can just compute both values and compare them. Run the following Python script with different N, and you’ll get a feeling for just how accurate the approximation is:
import math
N = 1000000
probUnique = 1.0
for k in xrange(1, 2000):
    probUnique = probUnique * (N - (k - 1)) / N
    print k, 1 - probUnique, 1 - math.exp(-0.5 * k * (k - 1) / N)
Great, so this magic expression serves as our probability that all values are unique. Subtract it from one, and you have the probability of a hash collision:
1 - e^{\frac{-k(k-1)}{2N}}
Here is a graph for N = 2^{32}. This illustrates the probability of collision when using 32-bit hash values. It’s worth noting that a 50% chance of collision occurs when the number of hashes is 77163. Also note that the graph takes the same S-curved shape for any value of N.

Monday, 10 September 2012

NO OF BST POSSIBLE ON N NUMBERS??

THIS MAY HELP!!!

the number of binary search trees with n nodes, C(n), is the nth Catalan number. Here's why:

Consider the integers 1, 2, ..., n. The number of trees with root k is C(k-1)C(n-k), because there are k-1 integers below k and n-k above k. Then the total is found by adding these up for k from 1 to n:


C(n) = C(0)C(n-1) + C(1)C(n-2) + ... + C(n-1)C(0)

with the trivial cases given by C(0)=1 and C(1)=1.