Sunday 24 March 2013

C PROGRAM TO TEST WHETHER A NUMBER IS PRIME OR NOT!!!


I came across a one line formula for testing primeness of a number in Theory of Computation paper. Its of the form...

αp-1 % p ≡1   if p is prime where α € Z+ and p is a number to be checked for its primeness.


PROGRAM:

#include<stdio.h>

int main()
{
    int p,temp;
    printf("enter the number\n");
    scanf("%d",&p);
    
    if(p==1)       printf("number is neither prime nor composite\n");
    if(p==2)       printf("number is prime\n");
    
    temp=pow(2,p-1);    // i've taken alpha as 2
    if(temp%p==1)        printf("%d is prime",p);
    else        printf("%d is not prime",p);

    getch();
    return 0;
}

Saturday 23 March 2013



KADANE'S ALGORITHM:

Kadane's algorithm is an efficient solution for Maximum subarray problem with runtime complexity of O(n). This algorithm is an example of Dynamic Programming(the maximum subarray ending at each position is calculated in a simple way from a related but smaller and overlapping subproblem, the maximum subarray ending at the previous position) 

Maximum subarray problem:

Given an array, our task is to find a contiguous subarray whose sum is maximum. For eg. consider an array {-1,4,-2,5,-5,2,-20,6}. The solution  is a subarray {4,-2,5} whose sum is 7 which is the maximum sum possible from the given array.

Algorithm:


Kadane's Algorithm(array[1..n])
begin
    (maxSum, maxStartIndex, maxEndIndex) := (-INFINITY, 0, 0)
    currentMaxSum := 0
    currentStartIndex := 1
    for currentEndIndex := 1 to n do
        currentMaxSum := currentMaxSum + array[currentEndIndex]
        if currentMaxSum > maxSum then
            (maxSum, maxStartIndex, maxEndIndex) := (currentMaxSum, currentStartIndex, currentEndIndex)
        endif

        if currentMaxSum < 0 then
            currentMaxSum := 0
            currentStartIndex := currentEndIndex + 1
        endif
    endfor

    return (maxSum, maxStartIndex, maxEndIndex)
end


Program:



#include<stdio.h>

int main()
{
    int maxSum=log(0), maxStartIndex=0, maxEndIndex=0;
    //I'VE USED log(0) TO REPRESENT MINUS INFINITY.
    //THIS IS THE WAY OF REPRESENTING MINUS INFINITY 
    //IN C :-)
    int array[]={-1,4,-2,5,-5,2,-20,6};
    int currentMaxSum=0,currentStartIndex= 0, currentEndIndex=0;
    
    for(currentEndIndex=0;currentEndIndex<8;currentEndIndex++)
    {
        currentMaxSum = currentMaxSum + array[currentEndIndex];
        if(currentMaxSum > maxSum)
        {
            maxSum=currentMaxSum;
            maxStartIndex=currentStartIndex;
            maxEndIndex=currentEndIndex;
        }
        
        if(currentMaxSum < 0)
        {
            currentMaxSum= 0;
            currentStartIndex = currentEndIndex + 1;
        }
    }
    printf("Max sum=%d\nStart Index=%d\nEnd Index=%d",                maxSum,maxStartIndex,maxEndIndex);
        
    getch();
    return 0;
}


Output:
Max sum=7
Start Index=1
End Index=3













Sunday 10 March 2013

INSERTION SORT

In this sorting algorithm, in the 1st iteration, the 1st and 0th element of the array are compared. During the 2nd iteration, 2nd element is compared with 0th and 1st element. In general, nth element is compared with all the elements before it. (i.e) from 0th till (n-1)th element. If the nth element is greater than any other element appearing before it, say kth element, then all the elements from kth position till (n-1)th position are shifted to the next position. The nth element is then swapped with the kth element.



PROGRAM:

#include <stdio.h>

void print(int *p,int n)
{
    int j=0;
    while(j<n)
    {
        printf("%d",p[j]);
        j++;
    }
    printf("\n");
}

int main() 
{
   
    int array_size,*array, i,temp,j,k;
    printf("Enter the array size\n");
    scanf("%d", &array_size);
    array=(int*)malloc(sizeof(int)*array_size);
    printf("Enter the array elements\n");
    for(i=0;i<array_size;i++) 
    { 
       scanf("%d",&array[i]); 
    }
    printf("Sorting...\n");
    for(i=1;i<array_size;i++)
    {
        for(j=0;j<i;j++)
        {
            
            if(array[j]>array[i])
            {
                temp=array[i];
                for(k=i;k>j;k--)   array[k]=array[k-1];
                array[j]=temp;
            }
        }
        print(array,array_size);
    }
   return 0;
}

OUTPUT:
Enter the array size
9
Enter the array elements
9 8 6 7 3 5 4 1 2

Sorting...

8 9 6 7 3 5 4 1 2
6 8 9 7 3 5 4 1 2
6 7 8 9 3 5 4 1 2
3 6 7 8 9 5 4 1 2
3 5 6 7 8 9 4 1 2
3 4 5 6 7 8 9 1 2 
1 3 4 5 6 7 8 9 2
1 2 3 4 5 6 7 8 9




<br />


Thursday 7 March 2013

QUEUE

IMPLEMENTATION:

front=rear=-1;
int queue[MAX];

void enqueue(int element)
{
       if(rear==(MAX-1))
{
                printf(“Queue is full!\n”);
                return;
}
else if(rear=-1 && front=-1)
{
front++;
rear++;
        queue[rear]=element;
}
else
{
rear++;
queue[rear]=element;
}
}

int dequeue()
{
        int data;
        if(front==-1)
{
                printf(“Queue is empty!\n”);
                return;
}
else if(front==rear)
{
                data=queue[front];
                front=rear=-1;
                return data;
}
else
{
data=queue[front];
front++;
return data;
}
}
STACK

IMPLEMENTATAION:

top=-1;
int stack[MAX];

void push(int element)
{
        if(top==(MAX-1))
{
                printf(“Stack overflow!\n”);
                return;
}
else
{
                top++;
                stack[top]=element;
}
}

int pop()
{
        int data;
        if(top==-1)
{
                printf(“Stack underflow!\n”);
                return;
}
else
{
data=stack[top];
top--;
return data;
}
}

    

Wednesday 6 March 2013

MULTIPLICATION OF NxN MATRIX:

Matrix multiplication is possible only if the no. of columns in matrix1 is equal to the no. of rows in matrix2.

(i.e) matrix1 -  M x N
       matrix2 -  N x P
The resultant matrix order will be M x P.

Basic step:

for(i=0;i<row1;i++)  //row1- no. of rows in matrix1
{
    for(j=0;j<col2;j++)   //col2- no. of columns in matrix2
    {
         mult[i][j]=0;
         for(k=0;k<row1;k++)
             mult[i][j]+=matrix1[i][k]*matrix2[k][j];
         printf("%d\t",mult[i][j]);
     }
     printf("\n");
}
              

Sunday 27 January 2013

GOOD BOOKS FOR DATA STRUCTURE:

1) Data Structures and Algorithms using C by Amitava Nag and 
Jyoti Prakash Singh
2) Data Structures using C by Amiya Kumar Rath and Alok Kumar Jagadev