PART A
1.Write a C program to perform Linear Search.
#include <stdio.h>
#include<conio.h>
void main() {
int n, i, key, result = 0,arr[10];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element found at index %d\n", i);
result = 1;
break;
if (result==0) {
printf("Element not found in the array.\n");
getch();
}
2. Write a program to find factorial of number using recursion.
#include <stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==0)
return 1;
else
return fact(n-1)*n;
}
void main()
{
int num;
printf(“enter a number\n”);
scanf(“%d”,&num);
result=fact(num);
printf(“the factorial of a number is %d\n”,result);
getch();
}
3. Write a program to find Fibonacci of a number using recursion.
#include<stdio.h>
#include<conio.h>
int fibonacci(int n) {
if (n <= 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
// Taking user input
printf("Enter the number of terms: ");
scanf("%d", &n);
// Printing Fibonacci series
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
4. }Write a program to sort the elements using Selection sort.
#include <stdio.h>
#include<conio.h>
void main( )
int arr[10],n;
int i, j, position, swap;
printf(“Enter size of array\n”);
scanf(“%d”,&n);
printf(“Enter elements of array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
if (position != i)
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
printf(“sorted elements are\n”);
for (i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
getch();
5.Write a program to sort the elements using Bubble sort
#include <stdio.h>
#include<conio.h>
void main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
getch();
}
6.Write a program to perform Graph traversal using Depth First Search.
#include<stdio.h>
#include<conio.h>
int c[10][10],i,j,k,m,n,stk[10],top,v,visit[10],visited[10];
void main()
printf("Enter the no of vertices\n");
scanf(“%d”,&n);
printf("Enter the no of edges\n");
scanf(“%d”,&m);
printf("Enter the edges \n");
for(k=1;k<=m;k++)
scanf(“%d%d”,&i,&j);
c[i][j]=1;
printf("Enter the initial vertex\n");
scanf(“%d”,&v);
printf("Order of visited vertices\n");
printf(“%d”,v);
visited[v]=1;
k=1;
while(k<n)
for(j=n;j>=1;j--)
if(c[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
printf(“%d”,v)
k++;
visit[v]=0;
visited[v]=1;
}
getch();
OUTPUT
Enter the no of vertices3
Enter the no of edges3
Enter the edges
12
13
23
Enter the initial vertex1
Order of visited vertices
1 23
7.Write a program to perform Graph traversal using Breadth First Search.
#include<stdio.h>
#include<conio.h>
int c[10][10], i, j, k, m, n, q[10],front, rear, v, visit[10],visited[10];
void main()
{
printf("enter the no of vertices\n");
scanf(“%d”,&n);
printf(“"enter the no of edges\n");
scanf(“%d”,&m);
printf("Enter the edges\n");
for(k=1;k<=m;k++)
{
scanf(%d%d”,&i,&j);
c[i][j]=1;
}
printf("enter initial vertex\n");
scanf(“%d”,&v);
printf("Order of visited vertices\n");
printf(“%d”,v);
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
{
if(c[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
q[rear++]=j;
}
}
v=q[front++];
printg(v);
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}
OUTPUT
enter the no of vertices4
enter the no of edges4
Enter the edges
12
13
24
34
enter initial vertex 1
Order of visitied vertices
1 2 3 4
8.Write a program to implement Binary Search using Divide and Conquer
technique.
#include <stdio.h>
#include<conio.h>
int BinarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
void main()
{
int n, a[10];
printf(“enter size of array\n”);
scanf(“%d”,&n);
printf(“enter elements of array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
Printf(“enter key element\n”);
Scanf(“%d”,&key);
int result = binarySearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
PART B
1. Write a program to implement Knapsack problem using Greedy
Technique.
#include<stdio.h>
#include<conio.h>
void main()
{
float w[50],p[50],r[50],totalvalue=0,temp,c;
int n,i,j;
printf("Enter the number of items :\n");
scanf(“%d”,&n);
for (i = 0; i< n; i++)
{
printf("Enter the profit and Weight for item\n”,i);
pcanf(“%d”,&p[i]);
scanf(“%d”,&w[i]);
}
printf("Enter the capacity of knapsack :\n");
scanf(“%d”,&c);
for(i=0;i<n;i++)
r[i]=p[i]/w[i];
for (i = 0; i< n; i++)
{
for (j = i + 1; j < n; j++)
{
if (r[i] < r[j])
{
temp = r[j];
r[j] = r[i];
r[i] = temp;
temp = w[j];
w[j] = w[i];
w[i] = temp;
temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
printf("Knapsack capacity using Greedy Algorithm:\n");
for (i = 0; i< n; i++)
{
if (w[i] > c)
break;
else
{
totalvalue = totalvalue + p[i];
c = c - w[i];
}
}
if (i< n)
totalvalue = totalvalue + (r[i]*c);
printf(“the maximum value is :\n”+totalvalue);
getch();
}
OUTPUT
Enter the number of items :3
Enter the profit and Weight for item[0]=
20 5
Enter the profit and Weight for item[1]=
42
Enter the profit and Weight for item[2]=
64
Enter the capacity of knapsack :
10
Knapsack capacity using Greedy Algorithm:
The maximum value is =28.5
2. Write a program to perform Merge sort on array elements.
#include <stdio.h>
#include<conio.h>
void merge(int [], int, int, int);
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);
}
}
void merge(int a[20], int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
void main()
{
int a[30], n;
printf("Enter the size: ");
scanf("%d", &n);
printf("Enter the array elements: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
mergesort(a, 0, n - 1);
printf("Sorted array:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
getch();
}
OUTPUT
Enter the size 4
Enter the array elements 5 6 2 8
Sorted array
2 568
3.Write a progam to sort the array elements using Quick sort .
#include <stdio.h>
#include<conio.h>
void quicksort(int a[], int low, int high);
int partition(int a[],int low,int high)
{
int i.j.temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&key>=a[i])
i++;
while(key<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
Void quicksort(int a[],int low,int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}
void main()
{
int a[20], n, i;
printf("Enter the no of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
quicksort(a, 0, n - 1);
printf("Sorted elements are:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
4. Write a program to estimate a path of a graph using Prim’s algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,u,v,n,i,j,ne=1,visited[10]={0},min,mincost=0,cost[10][10];
printf("Enter the number of vertices:\n");
scanf(“%d”,&n);
printf("Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
Scanf(“%d”,&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf(“\n”)
while(ne< n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("Edge %d:(%d %d) cost:%d\n", ne++, a, b, min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"Minimun cost="<<mincost;
getch();
}
OUTPUT
Enter the number of vertices:3
Enter the adjacency matrix:
0 2 12
2 0 20
12 20 0
Edge 1:(1 2)cost:2
Edge 2:(1 3)cost:12
Minimun cost=14