Anda di halaman 1dari 33

1.

Implementation of Merge Sort

PROGRAM:
#include<stdio.h>

void Merge(int *a,int low,int high,int mid);

void MergeSort(int *a,int low,int high);

int main()

{ int n,i,arr[100];

printf("Enter array

size\n"); scanf("%d",&n);

printf("enter the elements in the

array\n"); for(i=0;i<n;i++)

scanf("%d",&arr[i]);

MergeSort(arr,0,n-1);

printf("sorted data using Merge sort\n");

for(i=0;i<n;i++)

printf("%d\t",arr[i]);

return 0;

void Merge(int *a,int low,int high,int mid)

int i,j,k,temp[high-low+1];

i=low;k=0;j=mid+1;

while(i<=mid&&j<=high)

{ if(a[i]<a[j])

{
temp[k]=a[i];

k++;

i++;

else

temp[k]=a[j];

k++;

j++;

while(i<=mid)

temp[k]=a[i];

k++;

i++;

while(j<=high)

temp[k]=a[j];

k++;

j++;

for(i=low;i<=high;i++)

a[i]=temp[i-low];

}
}
void MergeSort(int *a,int low,int high)

{ int mid;

if(low<high)

mid=(low+high)/2;

MergeSort(a,low,mid);

MergeSort(a,mid+1,high);

Merge(a,low,high,mid);

}
2. Implementation of Quick sort

PROGRAM:
#include<stdio.h>
void swap(int*,int*);
int partition(int [],int,int);
void quickSort(int [],int,int);
int main()
{
int n,i,a[100];
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements in the array\n");
for( i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("sorting using quick sort\n");
quickSort(a,0,n-1);
printArray(a,n);
return 0;
}

void swap(int* a,int* b)


{
int t=*a;*a=*b;*b=t;
}
int partition(int a[],int low,int high)
{
int pivot=a[high],j;
int i=(low-1);
for( j=low;j<=high-1;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]);
return(i+1);
}
void quickSort(int a[],int low,int high)
{
if(low<high)
{
int pi=partition(a,low,high);
quickSort(a,low,pi-1);
quickSort(a,pi+1,high);
}
}
void printArray(int a[],int size)
{
int i;
for(i=0;i<size;i++)
printf("%d\t",a[i]);
}
3. To implement operations on
graph A.vertex insertion
B.vertex deletion
C. finding vertex
D. Edge addition and deletion
PROGRAM:
#include<stdio.h>
int n,i,j,ad[10][10];
char ver[20];
display()
{
printf("\n VERTEX\t ADJACENT
VERTICES\n"); printf("--------------------\n");
for(i=0;i<n;i++)
{
printf("%c\t",ver[i]);
for(j=0;j<n;j++)
{
if(ad[i][j]!=0)
printf("-->%c",ver[j]);
}
printf("\n");
}
}
int find(char v)
{
int k;
for(k=0;k<n;k++)
{
if (ver[k]==v)
return k;
}
return -1;

}
int main()
{
char s,d,v;
int op;
n=0;
do
{
printf("\n-----------OPERATIONS--------\n");
printf("[1]INSERT VERTEX\n [2]DELETE VERTEX\n [3]ADD AN EDGE\n [4]DELETE
AN EDGE\n [5]FIND VERTEX\n [6]DISPLAY\n [0] EXIT");
printf("\n choose an operation\n");
scanf("%d",&op);
switch(op)
{
case 1:printf ("\n enter vertex ");
fflush (stdin);
scanf ("%c",&v);
if(find(v)!=-1)
printf("\n vertex already exists");
else
{
ver[n++]=v;
printf("\n vertex inserted");

}
break;
case 2:if(n==0)
printf ("\n graph is empty");
else
{
i=n-1;
v=ver[n-1];
for(j=0;j<n;j++)
{
ver[i]=0;
ad[i][j]=ad[j][i]=0;
}
n--;
printf("\n vertex deleted is:%c",v);
}
break;
case 3:if(n==0)
printf("\n graph is empty");
else
{
printf("\n enter source vertex ");
fflush (stdin);
scanf("%c",&s);
printf("\n enter destination vertex ");
fflush(stdin);
scanf("%c",&d);
if(s!=d && find(s)!=-1&& find(d)!=-1)
{
i=find(s);
j=find(d);
if (ad[i][j]==0)
{
ad[i][j]=1;
printf("\n edge inserted \n");
}
else printf("\n already they are connected by an edge");

}
else
printf("\n edge is not connected\n");
}
break;
case 4:if(n==0)
printf("\n graph is empty");
else
{
printf("\n enter source vertex ");
fflush(stdin);
scanf("%c",&s);
printf("\n enter destination vertex ");
fflush(stdin);
scanf("%c",&d);
i=find(s);
j+find(d);
if (ad[i][j]!=1)
printf("\n there is no edge connecting them");
else
{
ad[i][j]=0;
printf("\n edge deleted\n");
}
}
break;
case 5:if(n==0)
printf("\n graph is empty");
else
{
printf("\n enter vertex");
fflush(stdin);
scanf("%c",&v);
if (find(v)!=-1)
printf("\n vertex found");
else
printf("\n vertex not found");
}
break;
case 6:if(n==0)
printf("\n graph is empty");
else
display();
break;
}
}while(op!=0);
}
4. Implement depth first search for a graph non recursively.

PROGRAM:
#include<stdio.h>
int am[10][10],n,v,visited[10],stk[10];
int i,j,top=-1;
int main()
{
printf("Enter Number of Vertices:");
scanf("%d",&n);
printf("\nEnter Adjacency Matrix:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&am[i][j]);
visited[i]=0;
}
printf("Enter Starting Node:");
scanf("%d",&v);
dfs(v);
}
dfs(int st)
{
top++;
stk[top]=st;
while(top>=0)
{
v=stk[top];
top--;
if(visited[v]==0)
{
visited[v]=1;
printf("-->%d",v);
}
for(i=n;i>=1;i--)
{
if(am[v][i]==1 && visited[i]==0)
{
if(instack(i)==0)
{
top++;
stk[top]=i;
}
}
}
}
}

int instack(int n)
{
for(j=0;j<=top;j++)
{
if(stk[j]==n)
return 1;
}
return 0;
}
5. Implement Breadth for search non

recursively. Program:

#include<stdio.h>
int am[10][10],n,v,visited[10],q[20],i,j,front=0,rear=0;
int main()
{
printf("Enter Number of Vertices:");
scanf("%d",&n);
printf("\nEnter Adjacency Matrix:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&am[i][j]);
visited[i]=0;
}
printf("enter the starting vertex");
scanf("%d",&v);
bfs(v);
getch();
}
bfs(int st)
{
q[rear]=st;
rear++;
while(front<rear)
{
v=q[front];
front++;
if(visited[v]==0)
{
visited[v]=1;
printf("-->%d",v);
}
for(i=1;i<=n;i++)
{
if(am[v][i]==1 && visited[i]==0)
{
if(inqueue(i)==0)
{
q[rear]=i;
rear++;
}
}
}
}
}
int inqueue(int n)
{

for(j=front;j<=rear;j++)
{
if(q[j]==n)
return 1;
}
return 0;
}
6.To implement functions of dictionary using hashing (division method ,
multiplication method,universal method hashing).

Program:
#include<stdio.h>
int htable[20],h,op,key,m,i,pos,count=0,flag;
int main()
{

printf("\nenter hash table size:");


scanf("%d",&m);
printf("--------------hash functions--------");
printf("\n1.division\n 2.multiplication\n 3.universal");
printf("\n choose hash function:");
scanf("%d",&h);
do
{
printf("\n------operations on dictionary------\n");
printf("\t1.insert\n\t 2.delete\n\t3.search\n\t4.display\n\t5.exit");
printf("\n enter operation:");
scanf("%d",&op);
switch(op)
{
case 1:if(count<m)
{
generatepos();
insert();
}
else printf("\nhash table is full");
break;
case 2:if(count==0)
printf("\n hash table is empty");
else
{
generatepos();
delete();
}
break;
case 3:if(count==0)
printf("\nhash table is empty");
else
{
generatepos();
if(search(key)==-1)
printf("\nkey is not found in the hash table");
else
printf("\n key found at location:%d",search(key));
}
break;
case 4:if(count==0)
printf("\n hash table is empty");
else
display();
break;
case 5: return(0);
}
}while(op!=0);
}
generatepos()
{
printf("enter key:");
scanf("%d",&key);
if(h==1)
pos=division(key);
else if(h==2)
pos=multiply(key);
else
pos=universal(key);
}
int division(int k)
{
return(k%m);
}
int multiply(int k)
{
float a=0.61804,t2;
int t1;
t1=k*a;
t2=(k*a);
t2=t2-t1;
pos=t2*m;
return pos;
}
int universal(int k)
{
int p=13,a=7,b=5;
pos=((a*k+b)%p)%m;
return pos;
}
insert()
{

flag=0;
if(search(key)==-1)
{
while(flag==0)
{
if(htable[pos]==0)
{
htable[pos]=key;
printf("\nkey inserted at location:%d",pos);
count++;
flag=1;
break;
}
else
{
printf("\ncollision occured at location:%d",pos);
pos=(pos+1)%m;
}
}
}
else printf("\n key is already present in the hash table");
}
delete()
{
flag=0;
if(search(key)!=-1)
{
while(flag==0)
{
if(htable[pos]==key)
{
htable[pos]=0;
printf("\nkey deleted from the location:%d",pos);
count--;
flag=1;
break;
}
else
pos=(pos+1)%m;

}
}
else printf("\nkey not found in the hash table");
}
int search(int k)
{
for(i=0;i<m;i++)
{
if(htable[i]==k)
return i;
}
return -1;
}
display()
{
printf("\n elements in the hash table are:\n");
for(i=0;i<m;i++)
printf("%d\t",htable[i]);
}
6.To perform various operations i.e., insertions and deletions on AVL trees
PROGRAM:
#include<stdio.h>
structavl
{
Struct avl *lchild,*rchild;
intdata,h;
};
#define max(a,b) (a>b?a:b)
int count=0;
int height(struct avl *root)
{
if(root==NULL)
return -1;
else
return root->h;
}
Int isEmpty(struct avl *p)
{
return(p==NULL && count==0);
}
Struct avl *findMin(struct avl *p)
{
if(p == NULL) return NULL;
else
{
if(p->lchild==NULL) return p;
else return findMin(p->lchild);
}
}
Struct avl *search( struct avl *p,int x)
{
Struct avl *t;
t = p;
while( t != NULL)
{
if(t->data == x)
return(t);
else if(t->data > x)
t = t->lchild; else
t = t->rchild;
}
return(NULL);
}
struct avl *rotateRight(struct avl *root)
{
struct avl *t;
t=root->lchild;
root->lchild=t->rchild;
t->rchild=root;
root->h=max(height(root->lchild),height(root->rchild))+1;
t->h=max(height(t->lchild),height(t->rchild))+1;
return t;
}
struct avl *rotateLeft(struct avl *root)
{
struct avl *t;
t=root->rchild;
root->rchild=t->lchild;
t->lchild=root;
root->h=max(height(root->lchild),height(root->rchild))+1;
t->h=max(height(t->lchild),height(t->rchild))+1;
return t;
}
struct avl *RightBal(struct avl *root)
{
Struct avl *t;
if(height(root->rchild)-height(root->lchild)==2)
{
t=root->rchild;
if((t->rchild->data>t->data) && (t->rchild!=NULL))
{
printf("\n\n\tRR(single left) Rotation");
root=rotateLeft(root);
}
else
{
printf("\n\n\tRL Rotation");
root->rchild=rotateRight(t);
root=rotateLeft(root);
}
}
return root;
}
struct avl *LeftBal(struct avl *root)
{
struct avl *t;
if(height(root->lchild)-height(root->rchild)==2)
{
t=root->lchild;
if((t->lchild->data<t->data) && (t->lchild!=NULL))

{
printf("\n\n\tLL(single right) Rotation");
root=rotateRight(root);
}
else
{
printf("\n\n\tLR Rotation");
root->lchild=rotateLeft(t);
root=rotateRight(root);
}
}
return root;
}
struct avl *insert(intx,struct avl *root)
{
if(root==NULL)
{
root=(struct avl *)malloc(sizeof(struct avl));
root->data=x;
root->h=0;

count++;
root->lchild=root->rchild=NULL;
printf("\n\tNode Inserted");
}
else if(x<root->data)
{
root->lchild=insert(x,root->lchild);
root=LeftBal(root);
}
else if(x>root->data)
{
root->rchild=insert(x,root->rchild);
root=RightBal(root);
}
root->h=max(height(root->lchild),height(root->rchild))+1;
return root;
}
struct avl *delete(intx,struct avl *p)
{
struct avl *temp;
if(x<p->data)
{
p->lchild = delete(x,p->lchild);
p=RightBal(p);
}
else if(x>p->data)
{
p->rchild = delete(x,p->rchild);
p=LeftBal(p);
}
else if(p->lchild&& p->rchild)
{
temp = findMin(p->rchild);
p->data = temp->data;
p->rchild = delete(p->data,p->rchild);
p=LeftBal(p);
}
else
{
temp = p;

if(p->lchild==NULL) p=p->rchild;
else if(p->rchild==NULL) p=p->lchild;
free(temp);
printf("\n\tNode Deleted");
p->h=max(height(p->lchild),height(p->rchild))+1;
count--;
}
return p;
}
void inorder(struct avl *p)
{
if(p!=NULL)
{
inorder(p->lchild);
printf("%4d",p->data);
inorder(p->rchild);
}
}
void main()
{
struct avl *root=NULL;
struct avl *t=NULL;
int ch,x;
printf("\t\t----------------LIST OF OPERATIONS------------------");
printf("\n\t\t\t[1].INSERT");
printf("\n\t\t\t[2].TRAVERSAL");
printf("\n\t\t\t[3].DELETE");
printf("\n\t\t\t[0].EXIT");
do
{
printf("\n\n\tEnter Choice Of Operation:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\tEnter Value:");
scanf("%d",&x);
if(search(root,x)==NULL)
root=insert(x,root);
else
printf("\n\tElement already present");
break;
case 2:if(!isEmpty(root))
{
printf("\n\tInorder Traversal:\t");
inorder(root);
}
else printf("\n\tAVL Tree Is Empty\n");
break;
case 3:if(!isEmpty(root))
{
printf("\n\tEnter Node To Be Deleted:");
scanf("%d",&x);
if(search(root,x)!=NULL)
root=delete(x,root);
else
printf("\n\tElement Not Found");
}
else printf("\n\tAVL Tree Is Empty\n");
break;
default:
if(ch!=0) printf("\n\tInvalid Choice Of Operation");
}
}while(ch!=0);
}
8. To perform various operations i.e, insertions and deletions on 2-3 trees

PROGRAM:
#include<stdio.h>
#define c 2
struct node
{
int data[c];
struct node *add[c+1];
}*root,*p,*l,*n,*n1;
int q[4],ele,i,j;
main()
{
int ch;
root=NULL;
clrscr();
while(1)
{
printf("\n1.insert \t2.display\t3.exit");
ptintf(“\n enter your choice: “);
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter the value to be inserted: ");
scanf("%d",&ele);
p=n1=NULL;
insert();
break;
case 2: printf(“\nElements are :”);
display(root);
printf("\n");
break;
case 3:exit();
default:printf("wrong choice…\n");
}
}
}
insert()
{
int temp;
printf("enter the element\n");
scanf("%d",&ele);
if(root==NULL)
{
n=(struct node *)malloc(sizeof(struct node));
n->data[0]=ele;
for(i=0;i<=c;i++)
n->add[i]=NULL;
n->data[1]=NULL;
root=n;
}
else
{
if(Search(p))
{
if(Is2Node(p))
{
p->data[1]=p->data[0];
if(ele<p->data[0])
p->data[0]=ele;
else
p->data[1]=ele;
p->add[1]=n1;
}
else
{
Split();
}
}
else
{
n=(struct node *)malloc(sizeof(struct node));
n->data[0]=ele;
n->data[1]=n->add[2]=NULL:
n->add[0]=p;
n->add[1]=n1;
root=n;
}
}
}
int Search(struct node *t)
{
l=root;
while(l!=t)
{
p=l;
for(i=0;i<c;i++)
{
if(l->data[i]!=NULL)
{
if(ele<l->data[i])
{
l=l->add[i];
break;
}
else if(i==1||l->data[i+1]==NULL)
{
l=l->add[i+1];
break;
}
}
}
}
if(p!=t)
return 0;
else
return 1;
}
int Is2Node(struct node *t)
{
for(i=0;i<c;i++)
if(t->data[i]==NULL)
return 0;
return 1;
}
Split()
{
int a,b,c;
a=Min(ele,p->data[0],p->data[1]);
b=Mid(ele,p->data[0],p->data[1]);
c=Max(ele,p->data[0],p->data[1]);
n=(struct node *)malloc(sizeof(struct node));
n->add[0]=p->add[2];
n->add[1]=n1;
n->add[2]=p->add[2]=NULL;
n->data[1]=p->data[1]=NULL;
p->data[0]=a;
n->data[0]=c;
n1=n;
ele=b;
insert();
}
int Min(int a,int b,int c)
{
if(a<b&&a<c)
return a;
else if(b<a&&b<c)
return b;
else
return c;
}
int Mid(int a,int b,int c)
{
if(a<b&&a>c||a>b&&a<c)
return a;
else if(b<a&&b>c||b>a&&b<c)
return b;
else
return c;
}
int Max(int a,int b,int c)
{
if(a>b&&a>c)
return a;
else if(b>a&&b>c)
return b;
else
return c;
}
display(struct node *t)
{
if(t!=NULL)
{
display(t->add[0]);
printf("%d\t",t->data[0]);
display(t->add[1]);
if(t->data[1]!=NULL)
{
printf("%d\t",t->data[1]);
display(t->add[2]);
}
}
}
9.To implement operations on binary heap.

PROGRAM:
#include<stdio.h>
int ch,i,heap[50],n,k,left,right,target,temp,parent;
int main()
{
printf("\n---------------HEAP OPERATIONS------------------");
printf("\n[1].BUILD_HEAP\n[2].INSERT\n[3].DELETEMIN\n[4].DISPLAY\n[0].EXIT"); do

{
printf("\nChoose Operation:");
scanf("%d",&ch);
switch(ch)
{
case 1: build_heap();
display();
break;
case 2: insert();
break;
case 3: deletemin();
break;
case 4: display();
break;
}
}
while(ch!=0);
}
build_heap()
{
printf("\nEnter No of Elements in Heap:");
scanf("%d",&n);
printf("\nEnter %d elements:\n",n);
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
for(i=n/2;i>0;i--)
percolate_down(i);
}
insert()
{
printf("Enter Element:");
scanf("%d",&temp);
n++;
heap[n]=temp;
printf("\nElement Inserted");
percolate_up(n);
}
deletemin()
{
if(n>0)
{
printf("\nElement Deleted Is:%d",heap[1]);
heap[1]=heap[n];
heap[n]=0;
n--;
percolate_down(1);
}
else printf("\nHeap Is Empty");
}
display()
{
if(n>0)
{
printf("\nThe Min-Heap is:\n");
i=1;
while(i<=n)
{
for(k=i;k<(2*i) && k<=n;k++)
printf("%d ",heap[k]);
printf("\n"); i=k;
}
}
else printf("\nHeap is Empty");
}
percolate_up(int m)
{
while(m>1)
{
parent=m/2;
if(heap[parent] > heap[m])
{
temp=heap[m];
heap[m]=heap[parent];
heap[parent]=temp;
m=parent;
}
else break;
}
}
percolate_down(int m)
{
while(2*m <= n)
{
left=2*m;
right=2*m+1;
if(right <=n && heap[right] < heap[left]) target=right;
else target=left;
if(heap[target] < heap[m])
{
temp=heap[m];
heap[m]=heap[target];
heap[target]=temp;
m=target;
}
else break;
}
}
10. Implementation of data searching using divides and conquers technique
PROGRAM:

#include<stdio.h>
#define size 50
int n;
void main()
{
int a[size],key,i,flag;
int binsearch(int a[size],int key);
clrscr();
printf("How many elements\n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the element which is to be search\n");
scanf("%d",&key);
flag=binsearch(a,key);
if(flag==-1)
{
printf("the element is not present\n");
}
else
printf("the element is at a[%d] location\n",flag);
getch();
}
int binsearch(int a[size],int key)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return mid;
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
11.Write a C program to implement pattern matching using Boyer-Moore algorithm.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char t[30],p[15];

int last[15];
main()
{
int pos,i;
clrscr();

printf("\n enter text: ");


scanf("%s",t);
printf("\n enter pattern: ");
scanf("%s",p);
/* calculate last function */

for(i=0;p[i]!='\0';i++)
last[p[i]]=i;
pos=pattern_match();
if(pos==-1)
printf("\n given pattern is not matched");

else

printf("\n pattern is matched at : %d",pos+1);


getch();

}
int pattern_match()

{
int n,m,i,j;
n=strlen(t);
m=strlen(p);
i=m-1;

j=m-1;
while(i<n)
{
if(t[i]==p[j])

{
if(j==0)
return i;
else
{
i--;
j--;
}
}

else
{
i=i+m-min(j,1+last[t[i]]);
j=m-1;
}

}
return(-1);
}
int min(int x,int y)
{

if(x<y)
return x;
else
return y;
}
12. Write a C program to implement Knuth-Morris-Pratt algorithm for pattern matching.

PROGRAM:
#include<stdio.h>
#include<string.h>
char t[30],p[15];
int f[15],i,j,n,m;
main()
{
int pos;
printf("\n enter text: ");
scanf("%s",t);
printf("\n enter pattern: ");
scanf("%s",p);
m=strlen(p);
n=strlen(t);
failurefunction();
pos=KMPmatch();
if(pos==-1)
printf("\n Pattern: %s Not Found",p);
else
printf("\n Pattern is matched at : %d",pos+1);

}
int KMPmatch()
{
i=0;
j=0;
while(i<n)
{
if(p[j]==t[i])
{
if(j==m-1)
return i-(m-1);
else
{
i++;
j++;
}
}
else if(j>0)

j=f[j-1];
else
i++;
}
return -1;
}
failurefunction()
{
i=1;
j=0;
f[0]=0;
while(i<m)
{
if(p[j]==p[i])
{
f[i]=j+1;
i++;
j++;
}
else if(j>0)
j=f[j-1];
else
{
f[i]=0;
i++;
}
}
}

Anda mungkin juga menyukai