1. Q1. Write a program to search a number from a given list using linear and binary
search.
#include<stdio.h>
#inlcude<conio.h>
void main()
{
int arr[10];
int i,j;
int n
int snumber;
int count=0;
printf("Enter the number of element in array");
scanf("%d",&n);
printf("nEnter the element in array");
for(i=0;i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("nenter the search element");
scanf("%d",&snumber);
for(i=0;i<n;i++)
{
if(arr[i]==snumber)
{
count=1;
printf("value is found");
break;
}
}
if(count==0)
printf("value is not found");
getch();
}
1
2. Output:-
Enter the number of element in array 5
Enter the element in array
3
7
8
9
5
enter the search element 9
value is found
Enter the number of element in array 5
Enter the element in array
3
7
8
9
4
enter the search element 10
value is not found
2
3. Q2. Write a program to search a number from a given list using binary search
recursively.
#include<stdio.h>
#include<conio.h>
binary(int,int,int);
int b[25];
void main()
{
int high,low=0,i,j,value,pos,n;
clrscr();
printf("Enter n number of item");
scanf("%d",&n);
high=n;
printf("Enter the value in sorted formn");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("Enter the value which you have to find");
scanf("%d",&value);
pos=binary(low,high,value);
//printf("n in location %d th",pos);
getch();
}
int binary(int low,int high,int value)
{
if(low==high)
{
if( b[(low+high)/2]==value)
{
printf("value is found");
return (low+high)/2;
}
else
{
printf("Value is not found");
return;
}
}
if( b[(low+high)/2]>value)
3
5. Q3. Write the program to find the factorial of number using both recursive and
non-recursive method and also compare that performance.
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
int i,fact=1;
clrscr();
printf("enter the number");
scanf("%d",&number);
for(i=number;i>1;i--)
{
fact=fact*i;
}
printf("nfactorial number of %d=%d",number,fact);
getch();
}
Output
enter the number 5
factorial number of 5=120
(with recursion)
#include<stdio.h>
#include<conio.h>
int fact(int);
int factn=1;
void main()
{
int number;
int i,factorial;
clrscr();
printf("enter the number");
scanf("%d",&number);
factorial=fact(number);
printf("nfactorial number of %d=%d",number,factorial);
getch();
}
int fact(int number)
5
7. Q4. Sort the following list in increasing order of number
9,94,45,47,28,98,65,42,8,4,88,6 using selection sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int sort[15];
int i,j;
int n;
int temp;
clrscr();
printf("enter the number of element in array");
scanf("%d",&n);
printf("nenter the value in array");
for(i=0;i<n;i++)
scanf("%d",&sort[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(sort[i]>sort[j])
{
temp=sort[i];
sort[i]=sort[j];
sort[j]=temp;
}
}
}
printf("nSorted array:");
for(i=0;i<n;i++)
printf("n%d",sort[i]);
getch();
}
7
8. Output
enter the number of element in array 12
enter the value in array 9 94 45 47 28 98 65 42 8 4 88 6
Sorted array:
4
6
8
9
28
42
45
47
65
88
94
98
8
9. Q5. Write a program to sort a list using merge sort by applying divide and conquer
method .
#include<stdio.h>
#include<conio.h>
int a[100];
void merge_sort(int a[],int left,int right);
void merge(int a[],int lb,int le,int rb,int re);
void main()
{
int i,num;
clrscr();
printf("enter the numn");
scanf("%d",&num);
printf("Enter the arrayn");
for(i=0;i<num;i++)
scanf("%d",&a[i]);
merge_sort(a,0,num-1);
printf("nSorted list is:n");
for(i=0;i<num;i++)
printf("%dn",a[i]);
getch();
}
void merge_sort(int a[],int left,int right)
{
int mid;
if(right>left)
{
mid=(right+left)/2;
merge_sort(a,left,mid);
merge_sort(a,mid+1,right);
merge(a,left,mid,mid+1,right);
}
}
void merge(int a[],int lb,int le,int rb,int re)
{
int na,nb,nc,k,c[30];
na=lb;
nb=rb;
nc=lb;
while((na<=le)&&(nb<=re))
{
if(a[na]<a[nb])
c[nc++]=a[na++];
else
c[nc++]=a[nb++];
9
11. Q6. Write a program to sort a list using quick sort by applying divide and conquer
method .
#include "stdio.h"
int split ( int*, int, int ) ;
void quicksort ( int *, int, int ) ;
void main( )
{
int arr[10];
int i ;
int n;
printf("enter the number of element in array");
scanf("%d",&n);
printf("Enter the value of array");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quicksort ( arr, 0, n ) ;
printf ( "nArray after sorting:n") ;
for ( i = 0 ; i < n ; i++ )
printf ( "%dt", arr[i] ) ;
getch();
}
void quicksort ( int a[ ], int lower, int upper )
{
int i ;
if ( upper > lower )
{
i = split ( a, lower, upper ) ;
quicksort ( a, lower, i - 1 ) ;
quicksort ( a, i + 1, upper ) ;
}
}
int split ( int a[ ], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
11
12. i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
OUTPUT
enter the number of element in array 5
Enter the value of array 1 4 3 5 2
Array after sorting:
1 2 3 4 5
12
13. Q7. Write a program to perform product of two matrix of order nxn using
strassen’s multiplication method.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[4][4],b[4][4],c[4][4],d[7],e[7],f[7],g[7],i,j,choice;
do
{
printf("n1. 4*4 matrix.");
printf("n2. 2*2 matrix.");
printf("n3. Exit.");
printf("nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("nEnter value for matrix A : ");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
printf("nEnter value for matrix B : ");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&b[i][j]);
d[0]=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
d[1]=(a[1][0]+a[1][1])*b[0][0];
d[2]=a[0][0]*(b[0][1]-b[1][1]);
d[3]=a[1][1]*(b[1][0]-b[0][0]);
d[4]=(a[0][0]+a[0][1])*b[1][1];
d[5]=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
d[6]=(a[0][1]-a[1][1])*(b[1][0]-b[1][1]);
e[0]=(a[0][2]+a[1][3])*(b[0][2]+b[1][3]);
e[1]=(a[1][2]+a[1][3])*b[0][2];
e[2]=a[0][2]*(b[0][3]-b[1][3]);
e[3]=a[1][3]*(b[1][2]-b[0][2]);
e[4]=(a[0][2]+a[0][3])*b[1][3];
e[5]=(a[1][2]-a[0][2])*(b[0][2]+b[0][3]);
e[6]=(a[0][3]-a[1][3])*(b[1][2]-b[1][3]);
f[0]=(a[2][0]+a[3][1])*(b[2][0]+b[3][1]);
f[1]=(a[3][0]+a[3][1])*b[2][0];
f[2]=a[2][0]*(b[2][1]-b[3][1]);
f[3]=a[3][1]*(b[3][0]-b[2][0]);
f[4]=(a[2][0]+a[2][1])*b[3][1];
f[5]=(a[3][0]-a[2][0])*(b[2][0]+b[2][1]);
13
19. Q9. Write a program to find minimum spanning tree using Prim’s method .
Code.
#include <stdio.h>
int n;
int weight[100][100];
char inTree[100];
int d[100];
int whoTo[100];
void updateDistances(int target) {
int i;
for (i = 0; i < n; ++i)
if ((weight[target][i] != 0) && (d[i] > weight[target][i])) {
d[i] = weight[target][i];
whoTo[i] = target;
}
}
int main(int argc, char *argv[]) {
FILE *f = fopen("dist.txt", "r");
fscanf(f, "%d", &n);
int i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
fscanf(f, "%d", &weight[i][j]);
fclose(f);
/* Initialise d with infinity */
for (i = 0; i < n; ++i)
d[i] = 100000;
/* Mark all nodes as NOT beeing in the minimum spanning tree */
for (i = 0; i < n; ++i)
inTree[i] = 0;
/* Add the first node to the tree */
printf("Adding node %cn", 0 + 'A');
inTree[0] = 1;
updateDistances(0);
int total = 0;
int treeSize;
for (treeSize = 1; treeSize < n; ++treeSize) {
/* Find the node with the smallest distance to the tree */
19
20. int min = -1;
for (i = 0; i < n; ++i)
if (!inTree[i])
if ((min == -1) || (d[min] > d[i]))
min = i;
/* And add it */
printf("Adding edge %c-%cn", whoTo[min] + 'A', min + 'A');
inTree[min] = 1;
total += d[min];
updateDistances(min);
}
printf("Total distance: %dn", total);
getch();
return 0;
}
20
24. Q11. Write a program to knapsack problem.
# include<stdio.h>
# include<conio.h>
void knapsack(int , float[], float[], float);
void main()
{
float weight[20], profit[20], capacity;
int n, i ,j;
float ratio[20], temp;
clrscr();
printf ("n Enter the no of objects:- ");
scanf ("%d", &n);
printf ("n Enter the profits and weight of each object:- ");
for (i=0; i<n; i++)
{
scanf("%f %f", &profit[i],&weight[i]);
}
printf ("n Enter the capacity of knapsack:- ");
scanf ("%f", &capacity);
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;
24
25. temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}
knapsack(n, weight, profit, capacity);
getch();
}
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp=0 ;
int i, j, u;
u=capacity;
for (i=0;i<n;i++)
x[i]=0.0;
for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}
if(i<n)
x[i]=u/weight[i];
tp= tp + (x[i]*profit[i]);
printf("n The result vector is:- ");
for(i=0;i<n;i++)
printf("%ft",x[i]);
printf("n Maximum profit is:- %f", tp);
}
25
26. Output
Enter the no of objects:- 3
Enter the profits and weight of each object:- 25 18
24 15
15 10
Enter the capacity of knapsack:- 20
The result vector is:- 1.000000 0.500000 0.000000
Maximum profit is:- 31.500000
26
27. Q12. Write a program to find the optimal sequence of performing the jobs to get
maximum profit.
void JS(int d[],int j[],int n,int p[])
{
int profit=0,i,q,m;
int k=1,r;
j[1]=1;
for(i=1;i<=n;i++)
{
r=k;
while((d[j[r]]>d[i]) && (d[j[r]]!=r))
r=r-1;
if(d[j[r]]<=d[i] && d[i]>r)
{
for(q=(k);q>=(r+1);q--)
{
if(q==-1)
break;
j[q+1]=j[q];
}
j[r+1]=i;
k=k+1;
}
}
for(m=1;m<=k;m++)
{
profit+=p[m];
}
printf("n MAXIMUM PROFIT: %d",profit);
}
void main()
{
int q,n,p[10],d[10],i,j[10];
clrscr();
printf("n enter no of processes : ");
scanf("%d",&n);
d[0]=j[0]=0;
for(i=1;i<=n;i++)
{
printf("n enter profit of process %d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
{
printf("n enter deadline ");
scanf("%d",&d[i]);
j[i]=0;
}
for(i=n-1;i>0;i--)
for(q=1;q<=i;q++)
if(p[q]<p[q+1])
27
29. Q13. Write a program to place the queen on a square chessboard using
Backtracking approach.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
int N; //For N * N ChessBoard
int flag;
void printArray(int a[]); /* Just to Print the Final Solution */
void getPositions(int a[],int n1,int n2); /* The Recursive Function */
int main()
{int *a;
int ctr=0;
printf("nTHE N QUEENS PROBLEM ");
printf("nNumber Of Rows(N) For NxN Chessboard.");
scanf("%d",&N);
a=(int *)(malloc(sizeof(int)*N));
printf("nAll possible Solutions .. n");
printf("nIn Each of the solutions the Coordinates of the N-Queens are given
(Row,Col) .");
printf("nNote that the Rows and Colums are numbered between 1 - N :n");
for(ctr=0;ctr<N;ctr++)
getPositions(a,0,ctr);
getchar();
getchar();
}
void printArray(int a[])
{
int i,choice;
static int counter=0;
counter++;
printf("nSOLUTION # %d :",counter);
for(i=0;i<N;i++)
printf("(%d,%d) ",i+1,a[i]+1);
if(counter%10==0) { printf("nEnter 0 to exit , 1 to continue .");
scanf("%d",&choice);
if(choice==0) exit(0);
};
}
void getPositions(int a1[],int colno,int val)
{ int ctr1,ctr2;
a1[colno]=val;
if(colno==N-1)
{ printArray(a1) ; return;
};
29
30. for(ctr1=0;ctr1<N;)
{ /* This Loop Finds Suitable Column Numbers , in the NEXT ROW */
for(ctr2=0;ctr2<=colno;ctr2++)
if(a1[ctr2]==ctr1 || (colno+1-ctr2)*(colno+1-ctr2)==(ctr1-a1[ctr2])*(ctr1-a1[ctr2]))
goto miss1;
getPositions(a1,colno+1,ctr1);
miss1:
ctr1++;
}
}
30
32. Q14. Write the program to solve Graph Coloring problem using Backtracking
approach.
#include<stdio.h>
#include<conio.h>
void colournext(int k,int colour[],int n,int m,int edge[20][20])
{
int flag=0,j;
do
{
flag=0;
colour[k]=(colour[k]+1)%(m+1);
if(colour[k]==0)
return;
for(j=0;j<n;j++)
{
if((edge[k][j]==1) && (colour[k]==colour[j]))
flag=1;
}
}while(flag==1);
}
void main()
{
int i,t,j,h,n,m,k,maxclique=0;
int edge[20][20];
int colour[20];
printf("n Enter the no. of vertices in the graph : ");
scanf("%d",&n);
for(i=0;i<n;i++)
colour[i]=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
edge[i][j]=0;
}
for(i=0;i<n;i++)
{
t=0;
printf("n Enter number of adjacent nodes from node %d : ",i);
scanf("%d",&t);
if(t>maxclique)
maxclique=t;
printf("n Enter those adjacent nodes : ");
32
34. Q15. Write a program of disjoint set union.
#include<stdio.h>
#include<conio.h>
int main()
{
int nlist,a[10],b[10],i,j,k,l,m;
printf("Enter total no of elements including all the listn");
scanf("%d",&j);
for(i=0;i<j;i++)
{
printf("Enter elementn");
scanf("%d",&a[i]);
printf("nEnter element's parentn");
scanf("%d",&b[i]);
}
printf("Elements are:n");
for(i=0;i<j;i++)
{
printf("%dt",a[i]);
}
printf("nparents are:n");
for(i=0;i<j;i++)
{
printf("%dt",b[i]);
}
printf("nPlease enter the set's element you want to joinn");
scanf("%d %d",&k,&l);
for(i=0;i<j;i++)
{
if((a[i]==l) && (b[i]==-1))
{
b[i]=k;
}
else if((a[i]==l) && (b[i]!=-1))
{
m=b[i];
for(i=0;i<j;i++)
{
if((a[i]==m) && (b[i]==-1))
{
b[i]=k;
}
}
}
}
34