Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Bansal Institute Of Engineering and Technology, Lucknow

(Dr. A.P.J. Abdul Kalam Technical University, Uttar Pradesh)

DESIGN AND ANALYSIS OF ALGORITHM


LAB (KCS-553)
Bachelor of Technology
in

Computer Science and Engineering

Submitted To: Submitted By:


MRS. LATA DUBEY VIKAS DEEP SHARMA

ASSISTANT PROFESSOR CSE SECTION – 3B

ROLL. NO. :2104220100112


INDEX

SR.NO PROGRAM NAME PAGE DATE TEACHER’S


NO. SIGN

01. PROGRAM FOR RESCURSIVE BINARY AND 01 - 03 08/09/2023


LINERAR SEARCH.

02. PROGRAM FOR HEAP SORT. 04 - 06 09/09/2023

03. PROGRAM FOR MERGE SORT 07 - 08 15/09/2023

04. PROGRAM FOR SELECTION SORT 09- 10 16/09/2023

05. PROGRAM FOR QUICK SORT 11 - 13 22/09/2023

06. KNAPSACK PROBLEM USING GREEDY SOLUTION 14 - 15 29/09/2023

07. PROGRAM FOR INSERTION SORT 16 - 17 06/10/2023

08. FIND MINIMUM SPANNING TREE USING 18 - 19 13/10/2023


KRUSKAL’S ALGORITHM

09. TO IMPLEMENT PROGRAM OF FLOYD WARSHALL 20 -21 20/10/2023


ALGORITHM

10. FIND MINIMUM COST SPANNIG TREE OF A GIVEN 22 -23 27/10/2023


UNDIRECTED GRAPH USING PRIMS ALGORITHM
PROGRAM- 01
Objective:- Implement the recursive binary search and linear search, determine the
time taken to search an element . Repeat the experiment for different values of n. The
number of element to be searched and plot a graph of the time taken versus n.

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("ENTER THE SIZE OF THE ARRAY:");


scanf("%d",&n);
printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}

printf("ENTER THE SEARCH KEY:");


scanf("%d",&search_key);

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;
}

void linear_search(int search_key,int array[100],int n)


{
int i,location;
for(i=1;i<=n;i++)
{
if(search_key == array[i])
{
location = i;
printf("______________________________________\n");
printf("The location of Search Key = %d is
%d\n",search_key,location);
printf("______________________________________\n");
}
}
}

void binary_search(int search_key,int array[100],int n)


{
int mid,i,low,high;
low = 1;
high = n;
mid = (low + high)/2;
i=1;
while(search_key != array[mid])
{
if(search_key <= array[mid])
{
low = 1;
high = mid+1;
mid = (low+high)/2;
}
else
{
2|P a g e
low = mid+1;
high = n;
mid = (low+high)/2;
}
}
printf("__________________________________\n");
printf("location=%d\t",mid);
printf("Search_Key=%d Found!\n",search_key);
printf("__________________________________\n");

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]);

printf("\nArray before sorting:\n");


print_array(arr, n);

heapsort(arr, n);

printf("\n\nArray after sorting:\n");


print_array(arr, n);
printf("\t\t By: Vikas Deep Sharma");
return 0;
}

void heapsort(int* arr, int n)


{

for (int i = n / 2 - 1; i >= 0; i--)


{
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--)
{

int temp = arr[i];


arr[i] = arr[0];
arr[0] = temp;

4|P a g e
heapify(arr, i, 0);
}
}

void heapify(int* arr, int n, int i)


{

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 (right < n && arr[right] > arr[largest])


{
largest = right;
}

if (largest != i)
{
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

heapify(arr, n, largest);
}
}

/* printf the array */


void print_array(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
5|P a g e
}
}

Output:-
Enter your 5 elements2
5
7
8
4

Array before sorting:


2 5 7 8 4

Array after sorting:


2 4 5 7 8

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>

void selectionSort(int arr[], int size);


void swap(int *a, int *b);

int main()
{
int array[10], i, size;
clock_t start, end;

printf("How many numbers you want to sort: ");


scanf("%d", &size);
printf("Enter %d numbers\t", size);
printf("\n");

for (i = 0; i < size; i++)


scanf("%d", &array[i]);

start = clock();

selectionSort(array, size);

end = clock();

printf("Sorted array is ");


for (i = 0; i < size; i++)
printf(" %d ", array[i]);

double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("\nTime taken: %f seconds\n", time_taken);


printf(“\t\t By: Vikas Deep Sharma”);

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]);
}
}
}

void swap(int *a, int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

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>

void quicksort(int[], int, int);


int partition(int[], int, int);

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: ");

for (i = 0; i < n; i++)


scanf("%d", &a[i]);

start = clock();

quicksort(a, 0, n - 1);

end = clock();

printf("The sorted array elements are:\n");

for (i = 0; i < n; i++)


printf("%d ", a[i]);

double time_taken = ((double)(end - start)) /


CLOCKS_PER_SEC;

11 | P a g e
printf("\nSorting time: %f seconds\n", time_taken);

printf("Do you wish to continue (0/1)? ");


scanf("%d", &ch);
}
printf("\t\t By: Vikas Deep Sharma");
return 0;
}
void quicksort(int a[], int low, int high)
{
int mid;
if (low < high)
{
mid = partition(a, low, high);
quicksort(a, low, mid - 1);
quicksort(a, mid + 1, high);
}
}
int partition(int a[], int low, int high)
{
int key, i, j, temp;
key = a[low];
i = low + 1;
j = high;

while (i <= j)
{
while (i <= high && key >= a[i])
i = i + 1;

while (key < a[j])


j = j - 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];

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("Knapsack problems using Greedy Algorithm:\n");


for (i = 0; i < n; i++)
{
if (weight[i] > capacity)
break;
else
{
14 | P a g e
Totalvalue = Totalvalue + profit[i];
capacity = capacity - weight[i];
}
}
if (i < n)
Totalvalue = Totalvalue + (ratio[i]*capacity);
printf("\nThe maximum value is :%f\n",Totalvalue);
printf("\t\t By: Vikas Deep Sharma");
return 0;
}

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:

The maximum value is :10.800000

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()
{

printf("\n\tImplementation of Kruskal's algorithm\n");


printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost 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;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n\n",ne++,a,b,min);
mincost +=min;
18 | P a g e
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
printf(“\t\t\t\t By: Vikas Deep Sharma”);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}

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>

void floydWarshall(int **graph, int n)


{
int i, j, k;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (graph[i][j] > graph[i][k] + graph[k][j])
graph[i][j] = graph[i][k] + graph[k][j];
}} }

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_Key(int key[], bool Min_Span_Tree[])

{
int least = INT_MAX, min_index;

for (int v = 0; v < Vertices; v++)

if (Min_Span_Tree[v] == false && key[v] < least)

least = key[v], min_index = v;

return min_index;

int print_Prims_MST(int parent[], int graph[Vertices][Vertices])

{
printf("Edge \tWeight\n");
for (int i = 1; i < Vertices; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}

void prims_MST(int graph[Vertices][Vertices])

{
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])

parent[v] = u, key[v] = graph[u][v];

printf("Created Spanning Tree for Given Graph is: \n");


printf("\n");
print_Prims_MST(parent, graph);
}

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

You might also like