ADA Manual Updated
ADA Manual Updated
ADA Manual Updated
SEMESTER: 5
LAB MANUAL
Analysis And Design of Algorithms
3150703
Name:
Enrollment No:
Batch:
CERTIFICATE
Examiner
Faculty of Degree Engineering – 083
Department of Information Technology - 016
INDEX
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter no of bubble sort");
scanf("%d",&a[i]);
}
for(i=1;i<=5;i++)
{
for(j=i+1;j<=5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t,s;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter no for selection sort ");
scanf("%d",&a[i]);
}
for(i=1;i<5;i++)
{
for(j=i+1;j<=5;j++)
{
s=i;
if(a[j]<a[s])
{
s=j;
}
t=a[i];
a[i]=a[s];
a[s]=t;
}
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],x;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter no for insertion sort ");
scanf("%d",&a[i]);
}
for(i=1;i<=5;i++)
{
x=a[i];
j=i-1;
while(j>=0 && a[j]>x)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=x;
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Signature:
Date: _________________________
#include<stdio.h>
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
void main ()
{
int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i;
mergeSort(a,0,9);
printf("printing the sorted elements");
for(i=0;i<10;i++)
{
printf("\n%d\n",a[i]);
}
}
void mergeSort(int a[], int beg, int end)
{
int mid;
if(beg<end)
{
mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
void merge(int a[], int beg, int mid, int end)
{
int i=beg,j=mid+1,k,index = beg;
int temp[10];
while(i<=mid && j<=end)
{
if(a[i]<a[j])
{
temp[index] = a[i];
i = i+1;
}
else
{
temp[index] = a[j];
j = j+1;
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
void main()
int arr[20], n, i;
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
quicksort(arr, 0, n-1);
printf("Sorted array\n");
getch();
pivot = low;
i = low;
j = high;
i++;
j--;
if(i < j)
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
return newNode;
}
return root;
}
int main(){
inorder(root);
}
Signature:
Date: _________________________
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
void maxheapify(int *, int, int);
int* buildmaxheap(int *, int);
void main()
{
int i, t, n;
int *a = calloc(MAX, sizeof(int));
int *m = calloc(MAX, sizeof(int));
printf("Enter no of elements in the array\n");
scanf("%d", &n);
printf("Enter the array\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
m = buildmaxheap(a, n);
printf("The heap is\n");
for (t = 0; t < n; t++) {
printf("%d\n", m[t]);
}
}
int* buildmaxheap(int a[], int n)
{
int heapsize = n;
int j;
for (j = n/2; j >= 0; j--) {
maxheapify(a, j, heapsize);
}
return a;
}
void maxheapify(int a[], int i, int heapsize)
{
int temp, largest, left, right, k;
left = (2*i+1);
right = ((2*i)+2);
if (left >= heapsize)
return;
else {
if (left < (heapsize) && a[left] > a[i])
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
#include<string.h>
long long int fact[10000];
//char f[10000];
int i,n;
int factorial(int k);
void main()
{
clrscr();
fact[0]=1;
printf("Enter Any Number Between 1 To 10 for factorial recursiove method:-
");
scanf("%d",&n);
factorial(n);
printf("fact=%lli",fact[n]);
getch();
}
int factorial(int k)
{
n = k;
for(i=1;i<=n;i++)
if(fact[i]==1)
{
return 0;
}
else
{
fact[i] = fact[i-1]*i;
}
return fact[i];
}
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i])); v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
printf("\nEnter the number of elements\n"); scanf("%d",&n);
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]); printf("Total profit = %d\n",profit);
getch();
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
void main()
{
int n, i;
scanf("%d",&n);
int arr[n++];
for(i=0;i<n;i++)
{
printf("enter the d %d : ",i);
scanf("%d",&arr[i]);
}
{
if(i == j)
{
return 0;
}
minimum = INT_MAX;
for(k=i;k<j;k++)
{
count = matrixChainOrder(p, i, k) + matrixChainOrder(p, k+1, j) + p[i-
1]*p[k]*p[j];
Signature:
Date: _________________________
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n, W, i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&value[i],&weight[i]);
}
scanf("%d",&W);
{
if(n==0 || W==0)
{
return 0;
}
{
if(a > b)
{
return a;
}
else
{
return b;
}
}
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
Signature:
Date: _________________________
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, ne = 1, i, j;
scanf("%d",&n);
printf("enter matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c[i][j]);
if(c[i][j] == 0)
{
c[i][j] = 999;
}
}
}
traversed[1] = 1;
while(ne < n)
{
minimum = 999;
for(i=1;i<=n;i++)
{
a = x = i;
b = y = j;
}
}
}
}
if(traversed[x] == 0 || traversed[y] == 0)
{
printf("%d edge(%d, %d) = %d\n",ne++, a, b, minimum);
traversed[b] = 1;
}
return 0;
}
Signature:
Date: _________________________
#include<stdio.h>
#include<conio.h>
int i,j,k,x,y,u,v,n,adj=1,min,minweight=0;
int weight[10][10],parent[10];
int find(int);
int uni(int,int);
int main()
{
printf("\nEnter the no. of vertices in graph:");
scanf("%d",&n);
printf("\nEnter the weight adjacency matrix:\n ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&weight[i][j]);
if(weight[i][j]==0)
{
weight[i][j]=999;
}
}
}
printf("The edges of minimum weight spanning tree are\n");
while(adj<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(weight[i][j]<min)
{
min=weight[i][j];
x=u=i;
y=v=j;
}
}
}
u=find(u);
Signature:
Date: _________________________
#include<stdlib.h>
#include<string.h>
int LCS();
int i, j, p, q;
void main()
{
printf("enter the first subsequence\n");
gets(G);
gets(H);
printf("LCS is : ");
LCS();
display(p, q);
}
int LCS()
{
p = strlen(G);
q = strlen(H);
for(i=0;i<=p;i++)
{
c[i][0] = 0;
for(i=0;i<=q;i++)
{
c[0][i] = 0;
}
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
if(G[i-1] == H[j-1])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 'c';
}
b[i][j] = 'u';
else
{
c[i][j] = c[i][j-1];
b[i][j] = 'l';
}
}
}
return 0;
}
{
if(i==0 || j==0)
if(b[i][j] == 'c')
{
display(i-1, j-1);
printf("%c",G[i-1]);
else
{
display(i, j-1);
}
return 0;
}
Signature:
Date: _________________________