Monday, 12 May 2014

Postfix Evaluation

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<conio.h>
void push(int);
int pop();
int stack[20],top=-1;
void main()
{
 char expr[20];
 int len,a,b,c,i,x;
 clrscr();
 printf("$$$ POSTFIX EVALUATION $$$");
 printf("\nEnter the postfix expression:");
 scanf("%s",expr);
 len=strlen(expr);
 for(i=0;i<len;i++)
 {
  if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
  {
   b=pop();
   a=pop();
   switch(expr[i])
   {
    case '+':c=a+b;
      push(c);
      break;
    case '-':c=a-b;
      push(c);
      break;
    case '*':c=a*b;
      push(c);
      break;
    case '/':c=a/b;
      push(c);
      break;
   }
  }
  else
  {
   printf("Enter the value of %c:",expr[i]);
   scanf("%d",&x);
   push(x);
  }
 }
 printf("The resultant value is : %d",stack[top]);
 getch();
}
void push(int item)
{
 stack[++top]=item;
}
int pop()
{
 int a;
 a=stack[top];
 top=top-1;
 return a;
}


OUTPUT:

$$$ POSTFIX EVALUATION $$$

Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500

Saturday, 10 May 2014

Merge Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
int a[20],temp[20];
void mergesort(int a[],int temp[],int n);
void msort(int a[],int temp[],int left,int right);
void merge(int a[],int temp[],int left,int center,int right);
void main()
{
                int i,n;
                clrscr();
                printf("$$$ MERGE SORT $$$");
                printf("\nEnter the array limit:");
                scanf("%d",&n);
                printf("Enter array elements:");
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                }
                mergesort(a,temp,n);
                printf("The Sorted elements are:\t");
                for(i=0;i<n;i++)
                {
                                printf("%d\t",a[i]);
                }
                getch();
}

void mergesort(int a[],int temp[],int n)
{
                msort(a,temp,0,n-1);
}

void msort(int a[],int temp[],int left,int right)
{
                int center;
                if(left<right)
                {
                                center=(left+right)/2;
                                msort(a,temp,left,center);
                                msort(a,temp,center+1,right);
                                merge(a,temp,left,center+1,right);
                }
}

void merge(int a[],int temp[],int left,int center,int right)
{
                int i,left_end,num_elements,tmp_pos;
                left_end=center-1;
                tmp_pos=left;
                num_elements=right-left+1;
                while((left<=left_end)&&(center<=right))
                {
                                if(a[left]<=a[center])
                                {
                                                temp[tmp_pos]=a[left];
                                                tmp_pos++;
                                                left++;
                                }
                                else
                                {
                                                temp[tmp_pos]=a[center];
                                                tmp_pos++;
                                                center++;
                                }
                }
                while(left<=left_end)
                {
                      temp[tmp_pos]=a[left];
                      left++;
                      tmp_pos++;
                }
                while(center<=right)
                {
                                temp[tmp_pos]=a[center];
                                center++;
                                tmp_pos++;
                }
                for(i=0;i<=num_elements;i++)
                {
                                a[right]=temp[right];
                                right--;
                }
}

OUTPUT:

$$$ MERGE SORT $$$

Enter the array limit:       8
Enter array elements:    40           20           70           14           60           61           97           30
The Sorted elements are:            14           20           30           40           60           61           70           97

Quick Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
void qsort(int a[],int,int);
void main()
{
                int i,a[20],n;
                clrscr();
                printf("$$$ QUICK SORT $$$");
                printf("\nEnter the array limit:");
                scanf("%d",&n);
                printf("Enter array elements:");
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                }
                qsort(a,0,n-1);
                printf("The Sorted elements are:\t");
                for(i=0;i<n;i++)
                {
                                printf("%d\t",a[i]);
                }
                getch();
}

void qsort(int a[],int left,int right)
{
                int i,j,pivot,temp;
                if(left<right)
                {
                                pivot=left;
                                i=left+1;
                                j=right;
                                for(;;)
                                {
                                                while(a[i]<=a[pivot])
                                                                i=i+1;
                                                while(a[j]>a[pivot])
                                                                j=j-1;
                                                if(i<j)
                                                {
                                                                temp=a[i];
                                                                a[i]=a[j];
                                                                a[j]=temp;
                                                }
                                                else
                                                {
                                                                break;
                                                }
                                }
                                temp=a[pivot];
                                a[pivot]=a[j];
                                a[j]=temp;
                                qsort(a,left,j-1);
                                qsort(a,j+1,right);
                }
}

OUTPUT:
$$$ QUICK SORT $$$
Enter the array limit:       8
Enter array elements:    40           20           70           14           60           61           97           30
The Sorted elements are:            14           20           30           40           60           61           70           97

Selection Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
                int i,j,k,n,c,a[20],min;
                clrscr();
                printf("$$$ SELECTION SORT $$$");
                printf("\nEnter the array limit:");
                scanf("%d",&n);
                printf("Enter array elements:");
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                }
                printf("Passes of selection sort:\n");
                for(i=0;i<n-1;i++)
                {
                                min=i;;
                                for(j=i+1;j<n;j++)
                                {
                                                if(a[min]>a[j])
                                                {
                                                                min=j;
                                                }
                                }
                                if(i!=min)
                                {
                                                k=a[i];
                                                a[i]=a[min];
                                                a[min]=k;
                                }
                                printf("\nPass=%d,\t",i+1);
                                for(c=0;c<n;c++)
                                {
                                                printf("%d\t",a[c]);
                                }
                }
                printf("\nThe sorted elements are:\t");
                for(i=0;i<n;i++)
                {
                                printf("%d\t",a[i]);
                }
                getch();
}

OUTPUT:

$$$ SELECTION SORT $$$
Enter the array limit:       6
Enter array elements:    23           78           45           8              32           56
Passes of selection sort:
Pass=1,                 8              78           45           23           32           56
Pass=2,                 8              23           45           78           32           56
Pass=3,                 8              23           32           78           45           56
Pass=4,                 8              23           32           45           78           56
Pass=5,                 8              23           32           45           56           78
The sorted elements are:             8              23           32           45           56           78

Insertion Sort


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
                int i,j,k,n,a[20],data;
                clrscr();
                printf("$$$ INSERTION SORT $$$");
                printf("\nEnter the array limit:");
                scanf("%d",&n);
                printf("Enter array elements:");
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                }
                printf("Passes of insertion sort:\n");
                for(i=1;i<n;i++)
                {
                                data=a[i];
                                for(j=i-1;(j>=0)&&(data<a[j]);j--)
                                {
                                                a[j+1]=a[j];
                                }
                                a[j+1]=data;
                                printf("\nPass=%d,\t",i);
                                for(k=0;k<n;k++)
                                {
                                                printf("%d\t",a[k]);
                                }
                }
                printf("\nThe sorted elements are:\t");
                for(i=0;i<n;i++)
                {
                                printf("%d\t",a[i]);
                }
                getch();
}


OUTPUT:

$$$ INSERTION SORT $$$
Enter the array limit:       6
Enter array elements:    23           78           45           8              32           56
Passes of insertion sort:
Pass=1,                 23           78           45           8              32           56
Pass=2,                 23           45           78           8              32           56
Pass=3,                 8              23           45           78           32           56
Pass=4,                 8              23           32           45           78           56
Pass=5,                 8              23           32           45           56           78
The sorted elements are:             8              23           32           45           56           78

Saturday, 26 April 2014

Bubble Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void bubble_sort(int [],int);
void main()
{
    int a[30],n,i;
    clrscr();
    printf("\n\n\n$$$  BUBBLE SORT  $$$\n\n\n");
    printf("\nEnter no of elements :");
    scanf("%d",&n);
    printf("\nEnter array elements :");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    bubble_sort(a,n);
    getch();
}
void bubble_sort(int a[],int n)
{
    int i,j,k,temp;
    printf("\nUnsorted Data:");
    for(k=0;k<n;k++)
    {
        printf("%5d",a[k]);
    }
    for(i=1;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
        printf("\nAfter pass %d : ",i);
        for(k=0;k<n;k++)
        {
            printf("%5d",a[k]);
        }
    }
}


OUTPUT:

$$$  BUBBLE SORT  $$$

Enter no of elements :5

Enter array elements :10    4    55    21    6

Unsorted Data: 10    4    55    21    6

After pass 1 : 4    10    21    6    55

After pass 2 : 4    10    6    21    55

After pass 3 : 4    6    10    21    55

After pass 4 : 4    6    10    21    55

Sorting

 PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n,t,j,a[20];
    clrscr();
    printf("\n\n\n $$$    Sorting     $$$ \n\n\n");
    printf("Enter the number of elements:\t");
    scanf("%d",&n);

    printf("Enter %d elements:\t",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n-1;i++)
    {
        for(j=i;j<=n-1;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }

    printf("Numbers in Ascending order:\t");
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }

    printf("\nNumbers in Descending order:\t");
    for(i=n-1;i>=0;i--)
    {
        printf("%d\t",a[i]);
    }
    getch();
}


OUTPUT:


$$$     Sorting      $$$

Enter the number of elements:   6

Enter 6 elements:            8              2              6              1              4              9

Numbers in Ascending order:     1       2       4       6       8       9

Numbers in Descending order:    9       8       6       4       2       1