Professional Documents
Culture Documents
BEVQMh KB
BEVQMh KB
BINARY SEARCH
#include <stdio.h>
#include <stdlib.h>
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter the number to be searched \n");
scanf("%d",&x);
binarysearch(arr,n,x);
return 0;
}
2. MERGE SORT
#include<stdio.h>
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
return 0;
}
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
3. MIN_MAX
#include<stdio.h>
#include<stdio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid;
if(i==j)
{
max = min = a[i];
}
else
{
if(i == j-1)
{
if(a[i] <a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid = (i+j)/2;
maxmin(i, mid);
max1 = max; min1 = min;
maxmin(mid+1, j);
if(max <max1)
max = max1;
if(min > min1)
min = min1;
}
}
}
int main ()
{
int i, num;
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
scanf ("%d",&a[i]);
max = a[0];
min = a[0];
maxmin(1, num);
printf ("Minimum element : %d\n", min);
printf ("Maximum element : %d\n", max);
return 0;
}
4. QUICK SORT
#include <stdio.h>
int main()
{
int n,i;
printf("Enter the number of Elements: \n");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
quicksort(arr,0,n-1);
printf("Sorted Array : \n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
5. CHAIN MATRIX
#include<stdio.h>
#include<limits.h>
return m[1][n-1];
}
int main()
{
int arr[] = {10,100,20,5,80};
int size =5;
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, size));
return 0;
}
6. FLOYD WARSHALL
#include<stdio.h>
#define V 4 //Change
void printSolution(int dist[][V])
{
int i,j;
printf ("Shortest Distance MAtrix:\n");
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][j] == 99999)
printf("%7s", "99999");
else
printf ("%7d", dist[i][j]);
}
printf("\n");
}
}
int main()
{
int graph[V][V] = { {0, 3, 99999, 99999}, //Change
{99999, 0, 12, 5},
{4, 99999, 0, -1},
{2, -4, 99999, 0}
};
floydWarshall(graph);
return 0;
}
7. BELLMAN FORD
#include <stdio.h>
#include <stdlib.h>
int Bellman_Ford(int G[20][20] , int V, int E, int edge[20][2])
{
int i,u,v,k,distance[20],parent[20],S,flag=1;
for(i=0;i<V;i++)
distance[i] = 1000 , parent[i] = -1 ;
S=1;
distance[S-1]=0 ;
for(i=0;i<V-1;i++)
{
for(k=0;k<E;k++)
{
u = edge[k][0] , v = edge[k][1] ;
if(distance[u]+G[u][v] < distance[v])
distance[v] = distance[u] + G[u][v] , parent[v]=u ;
}
}
for(k=0;k<E;k++)
{
u = edge[k][0] , v = edge[k][1] ;
if(distance[u]+G[u][v] < distance[v])
flag = 0 ;
}
if(flag)
for(i=0;i<V;i++)
printf("Vertex %d -> cost = %d parent =
%d\n",i+1,distance[i],parent[i]+1);
return flag;
}
int main()
{
int V,edge[20][2],i,j,k=0;
V=5; // Change
int G[20][20]={
{0,6,0,7,0},
{0,0,5,8,-4},
{0,-2,0,0,0}, //Change
{0,0,-3,0,9},
{2,0,7,0,0}
};
for(i=0;i<V;i++)
for(j=0;j<V;j++)
{
if(G[i][j]!=0){
edge[k][0]=i;
edge[k++][1]=j;
}
}
if(Bellman_Ford(G,V,k,edge))
printf("\nNo negative weight cycle\n");
else printf("\nNegative weight cycle exists\n");
return 0;
}
8. dIJKSTRA'S ALGO
#include <stdio.h>
#define IN 9999
int main()
{
int i,j,n,u;
n=8;
int G[8][8]={
{IN,3,2,5,IN,IN,IN,IN},
{3,IN,IN,IN,3,IN,IN,IN},
{2,IN,IN,IN,1,6,IN,IN },
{5,IN,IN,IN,IN,2,IN,IN},
{IN,3,1,IN,IN,IN,4,IN},
{IN,IN,6,2,IN,IN,1,4},
{IN,IN,IN,IN,4,1,IN,2},
{IN,IN,IN,IN,IN,4,2,IN}
};
u=0;
dijkistra(G,n,u);
}
9. 8-qUEEN
#include<stdio.h>
#include<math.h>
int board[20],count;
void queen(int row,int n);
int main()
{
int n,i,j;
return 1;
}
for(i=1;i<=n;++i){
printf("\n\n%d",i);
for(j=1;j<=n;++j){
if(board[i]==j)
printf("\tQ");
else
printf("\t-");
}
}
}
else
queen(row+1,n);
}
}
}
10. SALESMAN(DP)
#include<stdio.h>
int completed[10],n,cost=0;
n=4; //change
int ary[10][10]={
{0, 4, 1, 3},
{4 ,0, 2, 1}, //cange
{1,2,0,5},
{3,1,5,0}
};
completed[city]=1;
printf("%d--->",city+1);
for(i=0;i < n;i++)
{
if((ary[city][i]!=0)&&(completed[i]==0))
if(ary[city][i]+ary[i][city] < min)
{
min=ary[i][0]+ary[city][i];
kmin=ary[city][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
ncity=nc;
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int main()
{
int i;
for(i=0;i < n;i++)
completed[i]=0;
return 0;
}
11. FRACTIONAL KNAPSACK
#include <stdio.h>
}
printf("\n Maximum profit is : %f",tp);
}
int main()
{
int i,j;
float ratio[n],temp;
for(i=0;i<n;i++)
{
ratio[i]=(profit[i]/weight[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (ratio[i]<ratio[j])
{
temp = ratio[j];
ratio[j]=ratio[i];
ratio[i]=temp;
temp=weight[j];
weight[j]=weight[i];
weight[i]=temp;
temp=profit[j];
profit[j]=profit[i];
profit[i]=temp;
}
}
}
printf("After Sorting :\n Weight\tProfit\t\tW/P Ratio\n");
for(i=0;i<5;i++)
printf("%f\t%f\t%f\n",weight[i],profit[i],ratio[i]);
knapsack();
return 0;
}
12. BFS
#include <stdio.h>
int q[8], visited[8], n, i, j, f = 0, r = 0;
int a[8][8]={
{0,1,1,0,0,0,0,0},
{1,0,0,1,1,0,0,0},
{1,0,0,0,0,1,1,0},
{0,1,0,0,0,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,0,1,1,1,1,0}
};
void BFS(int v) {
for(i = 0; i < n; i++)
if(a[v][i]==1 && visited[i]!=1)
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
BFS(q[f++]);
}
}
int main()
{
int v=1;
n=8;
BFS(v);
for ( i =0; i <n; i++) {
if(visited[i]==1)
printf("%d\t",i+1);
}
}
13. DFS
#include<stdio.h>
int i,j,n=8;
int visited[8],n;
int G[8][8]={
{0,1,1,0,0,0,0,0},
{1,0,0,1,1,0,0,0}, //Change
{1,0,0,0,0,1,1,0},
{0,1,0,0,0,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,0,1,1,1,1,0}
};
void DFS(int i)
{
int j;
printf("%d\n",i+1);
visited[i]=1;
for(j=0;j<n;j++)
if(visited[j]!=1 && G[i][j]==1)
DFS(j);
}
void main()
{
DFS(0);
}