Sharma - DAA File PDF
Sharma - DAA File PDF
Program:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[100],search_key,i,j,n,low,high,location,choice;
void linear_search(int search_key,int array[100],int n);
void binary_search(int search_key,int array[100],int n);
printf("___________________\n");
printf("1.LINEAR SEARCH\n");
printf("2.BINARY SEARCH\n");
printf("___________________\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:
linear_search(search_key,array,n);
1|P a g e
break;
case 2:
binary_search(search_key,array,n);
break;
default:
exit(0);
}
printf("\t\t By: Vikas Deep Sharma");
return 0;
}
Output:-
ENTER THE SIZE OF THE ARRAY:3
ENTER THE ELEMENTS OF THE ARRAY:
1
2
3
ENTER THE SEARCH KEY:2
___________________
1.LINEAR SEARCH
2.BINARY SEARCH
___________________
ENTER YOUR CHOICE:1
______________________________________
The location of Search Key = 2 is 2
______________________________________
Output Screenshot:-
3|P a g e
PROGRAM- 02
Objective:- Sort a given set of elements using the heap sort method and determine the
time required sort the elements. Repeat the experiment for different values of n, the
number of elements in the list to be sorted and plot a graph of the time taken versus n.
Program:-
#include<stdio.h>
void heapify(int*,int, int);
void heapsort(int*, int);
void print_array(int*, int);
int main()
{
int arr[] = { 10, 30, 5, 63, 22, 12, 56, 33 };
int n = sizeof(arr) / sizeof(arr[0]);
heapsort(arr, n);
4|P a g e
heapify(arr, i, 0);
}
}
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
root or not
if (left < n && arr[left] > arr[largest])
{
largest = left;
}
if (largest != i)
{
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
Output:-
Enter your 5 elements2
5
7
8
4
Output Screenshot:-
6|P a g e
PROGRAM- 03
Objective:- Sort a given set of elements using Merge sort method and determine the time
taken to sort the elements. Repeat the experiment for different values of n, the number of
elements in the list to be sorted and plot a graph of the time taken versus n.
Program:-
#include<stdio.h>
#include<time.h>
#define MAX 20
void mergesort(int a[],int low,int high);
void merge(int a[],int low,int mid,int high);
void main() { int n,i,a[MAX],
ch=1;
clock_t start,end;
while(ch)
{
printf("Enter the number of elements :");
scanf("%d",&n);
printf("Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start=clock();
mergesort(a,0,n-1); end=clock();
printf("The sorted array is : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nTime taken =%lf",(end-start)/CLK_TCK);
printf("\nDo u wish to continue(0/1)\n");
scanf("%d",&ch);
printf(“\t\t By: Vikas Deep Sharma”);
}
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,mid,high);
7|P a g e
}
}
void merge(int a[],int low,int mid,int high)
{
int i,j,k,t[MAX];
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
if(a[i]<a[j])
t[k++]=a[i++];
else t[k++]=a[j++];
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=t[i];
}
Output:-
Enter the number of elements :3
Enter the elements : 8
4
9
The sorted array is : 4 8 9
Time taken =0.000000
Do u wish to continue(0/1)
0
Output Screenshot:-
8|P a g e
PROGRAM- 04
Objective:- Sort a given set of elements using Selection sort and hence find the time
required to sort elements. Repeat the experiment for different values of n, the number of
elements in the list to be sorted and plot a graph of the time taken versus n.
Program:-
#include <stdio.h>
#include <time.h>
int main()
{
int array[10], i, size;
clock_t start, end;
start = clock();
selectionSort(array, size);
end = clock();
9|P a g e
return 0;
}
void selectionSort(int arr[], int size)
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
}
Output:-
How many numbers you want to sort: 4
Enter 4 numbers
2
7
4
9
Sorted array is 2 4 7 9
Time taken: 0.000000 seconds
Output Screenshot:-
10 | P a g e
PROGRAM- 05
Objective:- Sort a given set of elements using Quick sort method and determine the time
taken to sort the elements. Repeat the experiment for different values of n, the number of
elements in the list to be sorted and plot a graph of the time taken versus n.
Program:-
#include <stdio.h>
#include <time.h>
int main()
{
int i, n, a[20], ch = 1;
clock_t start, end;
while (ch)
{
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the array elements: ");
start = clock();
quicksort(a, 0, n - 1);
end = clock();
11 | P a g e
printf("\nSorting time: %f seconds\n", time_taken);
while (i <= j)
{
while (i <= high && key >= a[i])
i = i + 1;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
temp = a[low];
12 | P a g e
a[low] = a[j];
a[j] = temp;
}
}
return j;
}
Output:-
Enter the number of elements: 4
Enter the array elements: 2
6
3
8
The sorted array elements are:
2368
Sorting time: 0.000000 seconds
Do you wish to continue (0/1)? 0
Output Screenshot:-
13 | P a g e
PROGRAM- 06
Objective:- Implement knapsack problem using greedy method.
Program :
#include<stdio.h>
int main()
{
float
weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;
int n,i,j;
printf("Enter the number of items :");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter Weight and Profit for item[%d] :\n",i);
scanf("%f %f", &weight[i], &profit[i]);
}
printf("Enter the capacity of knapsack :\n");
scanf("%f",&capacity);
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
Output:-
Enter the number of items :4
Enter Weight and Profit for item[0] :
1
2
Enter Weight and Profit for item[1] :
3
4
Enter Weight and Profit for item[2] :
5
6
Enter Weight and Profit for item[3] :
7
8
Enter the capacity of knapsack :
8
Knapsack problems using Greedy Algorithm:
Output Screenshot:-
15 | P a g e
PROGRAM- 07
Objective:- Sort a given set of elements using Insertion sort.
Program:-
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers.\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\t\t By: Vikas Deep Sharma");
return 0;
}
16 | P a g e
Output:-
Enter number of elements: 4
Enter 4 integers.
2
6
88
4
Sorted list in ascending order:
2 4 6 88
Output Screenshot:-
17 | P a g e
PROGRAM- 08
Objective:- Find Minimum Cost Spanning Tree of a given undirected graph using
Kruskal's algorithm
Program :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
return 0;
}
Output:-
Implementation of Kruskal's algorithm
Enter the no. of vertices:2
Enter the cost adjacency matrix:
1
2
3
4
The edges of Minimum Cost Spanning Tree are
1 edge (1,2) =2
Minimum cost = 2
Output Screenshot:-
19 | P a g e
PROGRAM- 09
Objective:-Write a program to implement all pair shortest path problem by using Floyd
Warshall algorithm.
Program :
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
int **graph = (int **)malloc((long unsigned) n * sizeof(int *));
for (i = 0; i < n; i++)
{
graph[i] = (int *)malloc((long unsigned) n * sizeof(int));
}
for (i = 0; i < n; i++)
{ for (j = 0; j < n; j++)
{ if (i == j)
graph[i][j] = 0;
else
graph[i][j] = 100;
}
}
printf("Enter the edges: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &graph[i][j]);
}
}
printf("The original graph is:\n");
20 | P a g e
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
floydWarshall(graph, n);
printf("The shortest path matrix is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
return 0;
Output:-
Enter the number of vertices: 2
Enter the edges:
[0][0]: 77
[0][1]: 5
[1][0]: 23
[1][1]: 1
The original graph is:
77 5
23 1
The shortest path matrix is:
28 5
23 1
Output Screenshot:-
21 | P a g e
PROGRAM- 10
Objective:-Find the minimum cost spanning tree of a given undirected graph using prim’s
algorithmm
Program :
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define Vertices 5
{
int least = INT_MAX, min_index;
return min_index;
{
printf("Edge \tWeight\n");
for (int i = 1; i < Vertices; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
{
int parent[Vertices];
int key[Vertices];
bool Min_Span_Tree[Vertices];
for (int i = 0; i < Vertices; i++)
key[i] = INT_MAX, Min_Span_Tree[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < Vertices - 1; count++)
{
22 | P a g e
int u = Least_Key(key, Min_Span_Tree);
Min_Span_Tree[u] = true;
for (int v = 0; v < Vertices; v++)
if (graph[u][v] && Min_Span_Tree[v] == false && graph[u][v] < key[v])
int main()
{
int graph[Vertices][Vertices] = {
{ 0, 3, 0, 6, 0 },
{ 3, 0, 4, 8, 5 },
{ 0, 4, 0, 0, 7 },
{ 6, 8, 0, 0, 11 },
{ 0, 5, 7, 11, 0 } };
prims_MST(graph);
printf("\t\t By: Vikas Deep Sharma");
return 0;
}
Output:-
Created Spanning Tree for Given Graph is:
Edge Weight
0-1 3
1-2 4
0-3 6
1-4 5
Output Screenshot:-
23 | P a g e
24 | P a g e