Friday, 16 May 2014

Radix Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
int arr[MAX];
int i, num,b[MAX], m = 0, exp = 1;
clrscr();
printf("$$$ RADIX SORT $$$");
printf("\nEnter the array limit:");
scanf("%d", &num);

printf("\nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}

for (i = 0; i < num; i++)
{
if (arr[i] > m)
m = arr[i];
}
while (m / exp > 0)
{
int box[10] = { 0 };
for (i = 0; i < num; i++)
{
box[arr[i] / exp % 10]++;
}
for (i = 1; i < 10; i++)
{
box[i] += box[i - 1];
}
for (i = num - 1; i >= 0; i--)
{
b[--box[arr[i] / exp % 10]] = arr[i];
}
for (i = 0; i < num; i++)
{
arr[i] = b[i];
}
exp *= 10;

printf("\nPass:");
for(i=0;i<num;i++)
{
printf("%d\t",arr[i]);
}
}
printf("\n\nThe sorted array is:\t");
for(i=0;i<num;i++)
{
printf("%d\t",arr[i]);
}
getch();
}


OUTPUT:

$$$ RADIX SORT $$$

Enter the array limit:7
Enter 7 Elements : 23    12    234    56    78    99    3456

Pass:    12     23      234     56      3456    78      99
Pass:    12     23      234     56      3456    78      99
Pass:    12     23      56      78      99      234     3456
Pass:    12     23      56      78      99      234     3456

The sorted array is:    12      23      56      78      99      234     3456

Merging Two List Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n,a[20],b[20],c[20];
clrscr();
printf("$$$ MERGING TWO LIST USING ARRAY $$$");
printf("\nEnter the size of the first array:");
scanf("%d",&n);
printf("Enter first sorted array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the size of the second array:");
scanf("%d",&m);
printf("Enter second sorted array elements:");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
i=j=k=0;
while(i<n && j<m)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
while(i<n)
{
c[k]=a[i];
k++;
i++;
}
while(j<m)
{
c[k]=b[j];
k++;
j++;
}
printf("Merging of two sorted array is:");
for(k=0;k<n+m;k++)
{
printf("%d\t",c[k]);
}
getch();
}

OUTPUT:

$$$ MERGING TWO LIST USING ARRAY $$$

Enter the size of the first array:4
Enter first sorted array elements:    12    23    35    47

Enter the size of the second array:3
Enter second sorted array elements:    27    68    77

Merging of two sorted array is:    12       23      27      35      47      68     77

Wednesday, 14 May 2014

Shell Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,k,n,a[20],data,p;
 clrscr();
 printf("$$$ SHELL 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 shell sort:\n");
 for(k=n/2;k>0;k=k/2)
 {
  for(i=k;i<n;i++)
  {
   data=a[i];
   for(j=i;(j>=k)&&(data<a[j-k]);j=j-k)
   {
    a[j]=a[j-k];
   }
   a[j]=data;
   printf("\nK=%d,\t",k);
   for(p=0;p<n;p++)
   {
    printf("%d\t",a[p]);
   }
   printf("\n");
  }
 }
 printf("\nThe sorted elements are:\t");
 for(i=0;i<n;i++)
 {
  printf("%d\t",a[i]);
 }
 getch();
}


OUTPUT:

$$$ SHELL SORT $$$
Enter the array limit:5
Enter array elements:44 22 66 11 55
Passes of shell sort:
K=2, 44 22 66 11 55
K=2, 44 11 66 22 55
K=2, 44 11 55 22 66

K=1, 11 44 55 22 66
K=1, 11 44 55 22 66
K=1, 11 22 44 55 66
K=1, 11 22 44 55 66
The sorted elements are:11 22 44 55 66

Circular Queue Implementation Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define SIZE 5
int count=0,front=-1,rear=-1,cq[SIZE];
void enqueue();
void dequeue();
void display();
void main()
{
 int ch;
 clrscr();
 printf("$$$ CIRCULAR QUEUE USING ARRAY $$$");
 while(1)
 {
  printf("\nCircular Queue operations are:");
  printf("\n1.enqueue\n2.dequeue\n3.display\n4.exit\n");
  printf("Enter your choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit(0);
   default:printf("Enter correct option");
    break;
  }
 }
}
void enqueue()
{
 int item;
 if(count==SIZE)
 {
  printf("Circular Queue is full");
 }
 else
 {
  printf("Enter the element to be inserted:");
  scanf("%d",&item);
  if(front==-1)
  {
   front=0;
  }
  rear=(rear+1)%SIZE;
  cq[rear]=item;
  count=count+1;
 }
}
void dequeue()
{
 if(count==0)
 {
  printf("Circular Queue is empty");
 }
 else
 {
  printf("The deleted element is %d",cq[front]);
  front=(front+1)%SIZE;
  count=count-1;
 }
}
void display()
{
 int i,j;
 if(count==0)
 {
  printf("Circular Queue is empty");
 }
 else
 {
  printf("Elements of circular queue:\n");
  j=front;
  for(i=0;i<count;i++)
  {
   printf("%d\n",cq[j]);
   j=(j+1)%SIZE;
  }
 }
}


OUTPUT:

$$$ CIRCULAR QUEUE USING ARRAY $$$
Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:11

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:22

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:33

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:44

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:55

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Circular Queue is full

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:3
Elements of circular queue:11 22 33 44 55

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:2
The deleted element is 11

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:66

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:3
Elements of circular queue:22 33 44 55 66

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:4

Monday, 12 May 2014

List ADT Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
int list[5],i,n=0;
void insert(int x,int p);
void del(int p);
void display();
void main()
{
 int x,p,c;
 clrscr();
 printf("$$$ LIST ADT USING ARRAY $$$");
 while(1)
 {
  printf("\nThe operations are:");
  printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n");
  printf("Enter your choice:");
  scanf("%d",&c);
  switch(c)
  {
   case 1:printf("Enter the element and position:");
    scanf("%d%d",&x,&p);
    insert(x,p);
    break;
   case 2:printf("Enter the position of element to be deleted:");
    scanf("%d",&p);
    del(p);
    break;
   case 3:display();break;
   case 4:exit(0);
  }
 }
}
void insert(int x,int p)
{
 if(n==5)
  printf("List is full");
 else if((p<0)||(p>n))
  printf("Position is invalid");
 else
 {
  for(i=n-1;i>=p;i--)
  {
  list[i+1]=list[i];
  }
  list[p]=x;
  n++;
 }
}
void del(int p)
{
 if(n==0)
  printf("List is empty");
 else if((p<0)||(p>n-1))
  printf("position is invalid");
 else
 {
  for(i=p;i<n;i++)
  list[i]=list[i+1];
  n--;
 }
}
void display()
{
 int p;
 if(n==0)
  printf("list is empty");
 else
 {
  printf("The elements present in the list are:\n");
  printf("\nElement\tPosition\n\n");
  for(p=0;p<n;p++)
  {
   printf("%d\t%d\n\n",list[p],p);
  }
 }
}


OUTPUT:

$$$ LIST ADT USING ARRAY $$$

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 11   0

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 22   1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 33   2

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 44   3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     22                1
     33                2
     44                3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 2
Enter the position of element to be deleted: 1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     33                1
     44                2

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 20   1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     20                1
     33                2
     44                3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 4

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