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