0% found this document useful (0 votes)
17 views

DAA FILE AVIRAL Chirag

The document contains the lab practical file submitted by Rehan Ahmad, a student of class G1-C with roll number 16139, for the subject Design and Analysis of Algorithms. It includes 13 experiments on different sorting and graph algorithms like binary search, heap sort, merge sort, selection sort, insertion sort, quick sort, knapsack problem, minimum spanning tree (using Kruskal's and Prim's algorithms), and travelling salesman problem. Each experiment has the code implementing the respective algorithm.

Uploaded by

jihives550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DAA FILE AVIRAL Chirag

The document contains the lab practical file submitted by Rehan Ahmad, a student of class G1-C with roll number 16139, for the subject Design and Analysis of Algorithms. It includes 13 experiments on different sorting and graph algorithms like binary search, heap sort, merge sort, selection sort, insertion sort, quick sort, knapsack problem, minimum spanning tree (using Kruskal's and Prim's algorithms), and travelling salesman problem. Each experiment has the code implementing the respective algorithm.

Uploaded by

jihives550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DRONACHARYA GROUP

OF INSTITUTIONS

B.TECH. COMPUTER SCIENCE


Vth SEMESTER

LAB PRACTICAL FILE : DESIGN AND ANALYSIS


OF ALGORITHM (KCS553)

Submitted to :
Prof. Niharika Namdev

NAME : REHAN AHMAD


CLASS : G1-C
ROLL NO. : 16139
INDEX

S.NO. EXPERIMENT SIGNATURE

1 Program for recursive binary and linear


search
2 Program for heap sort

3 Program for merge sort

4 Program for selection sort

5 Program for insertion sort

6 Program for quick sort

7 Program for knapsack problem using


greedy solution
8 Program for minimum spanning tree
using Kruskal’s algorithm
9 Program for travelling salesman
problem
10 Program for minimum spanning tree for
undirected graph using prim’s
algorithm
11 Program for 0-1 knapsack problem
using
(i) Dynamic programming
(ii) Greedy method
EXPERIMENT-1
Program for Recursive Binary & Linear Search

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2

if (arr[mid] == x)

return mid;

if (arr[mid] > x) {

return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);

return -1;

int Linear_search(int arr[], int Search_ele, int n)

int i;

static int temp=0;

if(n>0)

i=n-1;

if(arr[i]==Search_ele)

temp=1;

Linear_search(arr,Search_ele,i);

return temp;

}
int main(void)

int arr[] = { 2, 3, 4, 10, 40 };

int size = sizeof(arr) / sizeof(arr[0]);

int x = 10;

int index = binarySearch(arr, 0, size - 1, x);

if (index == -1) {

printf("Element is not present in array");

else {

printf("Element is present at index %d", index);

if(Linear_search(arr,Search_ele,n)==1)

printf("Element found....");

else

printf("Element not found....");

return 0;

}
EXPERIMENT-2
PROGRAM FOR HEAP SORT

#include <stdio.h>

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int N, int i)

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < N && arr[left] > arr[largest])

largest = left;

if (right < N && arr[right] > arr[largest])

largest = right;

if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, N, largest);

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

swap(&arr[0], &arr[i]);
heapify(arr, i, 0);

void printArray(int arr[], int N)

for (int i = 0; i < N; i++)

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

printf("\n");

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, N);

printf("Sorted array is\n");

printArray(arr, N);

}
EXPERIMENT-3
PROGRAM FOR MERGE SORT

#include <stdio.h>

void merge(int arr[], int p, int q, int r) {

int n1 = q - p + 1;

int n2 = r - q;

int L[n1], M[n2];

for (int i = 0; i < n1; i++)

L[i] = arr[p + i];

for (int j = 0; j < n2; j++)

M[j] = arr[q + 1 + j];

int i, j, k;

i = 0;

j = 0;

k = p;

while (i < n1 && j < n2) {

if (L[i] <= M[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = M[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {


arr[k] = M[j];

j++;

k++;

void mergeSort(int arr[], int l, int r) {

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

void printArray(int arr[], int size) {

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

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

printf("\n");

int main() {

int arr[] = {6, 5, 12, 10, 9, 1};

int size = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, size - 1);

printf("Sorted array: \n");

printArray(arr, size);

}
EXPERIMENT-4
PROGRAM FOR SLEECTION SORT

#include <stdio.h>

void swap(int* xp, int* yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void selectionSort(int arr[], int n)

int i, j, min_idx;

for (i = 0; i < n - 1; i++) {

min_idx = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

swap(&arr[min_idx], &arr[i]);

void printArray(int arr[], int size)

int i;

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

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

printf("\n");

int main()

int arr[] = { 64, 25, 12, 22, 11 };

int n = sizeof(arr) / sizeof(arr[0]);


selectionSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}
EXPERIMENT-5
PROGRAM FOR INSERTION SORT

#include <math.h>

#include <stdio.h>

void insertionSort(int arr[], int n)

int i, key, j;

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

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

void printArray(int arr[], int n)

int i;

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

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

printf("\n");

int main()

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;

}
EXPERIMENT-6
PROGRAM FOR QUICKSORT

#include <stdio.h>

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

int t = *a;

*a = *b;

*b = t;

int partition(int array[], int low, int high) {

int pivot = array[high];

int i = (low - 1);

for (int j = low; j < high; j++) {

if (array[j] <= pivot) {

i++;

swap(&array[i], &array[j]);

swap(&array[i + 1], &array[high]);

return (i + 1);

void quickSort(int array[], int low, int high) {

if (low < high) {

int pi = partition(array, low, high);

quickSort(array, low, pi - 1);

quickSort(array, pi + 1, high);

void printArray(int array[], int size) {

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

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

}
printf("\n");

int main() {

int data[] = {8, 7, 2, 1, 0, 9, 6};

int n = sizeof(data) / sizeof(data[0]);

printf("Unsorted Array\n");

printArray(data, n);

quickSort(data, 0, n - 1);

printf("Sorted array in ascending order: \n");

printArray(data, n);

}
EXPERIMENT-7
KNAPSACK PROBLEM USING GREEDY SOLUTION

#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

Totalvalue = Totalvalue + profit[i];

capacity = capacity - weight[i];

if (i < n)

Totalvalue = Totalvalue + (ratio[i]*capacity);

printf("\nThe maximum value is :%f\n",Totalvalue);

return 0;

}
EXPERIMENT-8
MINIMUM SPANNING TREE USING KRUSKAL’S ALGORITHM

#include <stdio.h>

#include <stdlib.h>

int comparator(const void* p1, const void* p2)

const int(*x)[3] = p1;

const int(*y)[3] = p2;

return (*x)[2] - (*y)[2];

void makeSet(int parent[], int rank[], int n)

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

parent[i] = i;

rank[i] = 0;

int findParent(int parent[], int component)

if (parent[component] == component)

return component;

return parent[component] = findParent(parent, parent[component]);

void unionSet(int u, int v, int parent[], int rank[], int n)

u = findParent(parent, u);

v = findParent(parent, v);

if (rank[u] < rank[v]) {

parent[u] = v;

else if (rank[u] > rank[v]) {


parent[v] = u;

else {

parent[v] = u;

rank[u]++;

void kruskalAlgo(int n, int edge[n][3])

qsort(edge, n, sizeof(edge[0]), comparator);

int parent[n];

int rank[n];

makeSet(parent, rank, n);

int minCost = 0;

printf( "Following are the edges in the constructed MST\n");

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

int v1 = findParent(parent, edge[i][0]);

int v2 = findParent(parent, edge[i][1]);

int wt = edge[i][2];

if (v1 != v2) {

unionSet(v1, v2, parent, rank, n);

minCost += wt;

printf("%d -- %d == %d\n", edge[i][0],

edge[i][1], wt);

printf("Minimum Cost Spanning Tree: %d\n", minCost);

int main()

int edge[5][3] = { { 0, 1, 10 },

{ 0, 2, 6 },

{ 0, 3, 5 },
{ 1, 3, 15 },

{ 2, 3, 4 } };

kruskalAlgo(5, edge);

return 0;

}
EXPERIMENT-9
PROGRAM FOR TRAVELLING SALESMAN PROBLEM

#include <stdio.h>

int matrix[25][25], visited_cities[10], limit, cost = 0;

int tsp(int c)

int count, nearest_city = 999;

int minimum = 999, temp;

for(count = 0; count < limit; count++)

if((matrix[c][count] != 0) && (visited_cities[count] == 0))

if(matrix[c][count] < minimum)

minimum = matrix[count][0] + matrix[c][count];

temp = matrix[c][count];

nearest_city = count;

if(minimum != 999)

cost = cost + temp;

return nearest_city;

void minimum_cost(int city)

int nearest_city;

visited_cities[city] = 1;
printf("%d ", city + 1);

nearest_city = tsp(city);

if(nearest_city == 999)

nearest_city = 0;

printf("%d", nearest_city + 1);

cost = cost + matrix[city][nearest_city];

return;

minimum_cost(nearest_city);

int main()

int i, j;

printf("Enter Total Number of Cities:\t");

scanf("%d", &limit);

printf("\nEnter Cost Matrix\n");

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

printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);

for(j = 0; j < limit; j++)

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

visited_cities[i] = 0;

printf("\nEntered Cost Matrix\n");

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

printf("\n");

for(j = 0; j < limit; j++)

printf("%d ", matrix[i][j]);


}

printf("\n\nPath:\t");

minimum_cost(0);

printf("\n\nMinimum Cost: \t");

printf("%d\n", cost);

return 0;

}
EXPERIMENT-10
PROGRAM FOR MINIMUM SPANNING TREE OF AN UNDIRECTED GRAPH USING
PRIMS ALGORITHM

#include <stdio.h>

#include <limits.h>

#define V 5

int minKey(int key[], int mstSet[]) {

int min = INT_MAX, min_index;

int v;

for (v = 0; v < V; v++)

if (mstSet[v] == 0 && key[v] < min)

min = key[v], min_index = v;

return min_index;

int printMST(int parent[], int n, int graph[V][V]) {

int i;

printf("Edge Weight\n");

for (i = 1; i < V; i++)

printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[V][V]) {

int parent[V];

int key[V], i, v, count;

int mstSet[V];

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

key[i] = INT_MAX, mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;

for (count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = 1;

for (v = 0; v < V; v++)


if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])

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

printMST(parent, V, graph);

int main() {

int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 },

{ 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 }, };

primMST(graph);

return 0;

}
EXPERIENT-11
(I) PROGRAM FOR 0-1 KNAPSACK PROBLEM USING DYNAMIC
PROGRAMMING

#include<stdio.h>

int max(int a, int b) { return (a > b)? a : b; }

int knapSack(int W, int wt[], int val[], int n)

int i, w;

int K[n+1][W+1];

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

for (w = 0; w <= W; w++)

if (i==0 || w==0)

K[i][w] = 0;

else if (wt[i-1] <= w)

K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);

else

K[i][w] = K[i-1][w];

return K[n][W];

int main()

int i, n, val[20], wt[20], W;

printf("Enter number of items:");

scanf("%d", &n);

printf("Enter value and weight of items:\n");

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

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

}
printf("Enter size of knapsack:");

scanf("%d", &W);

printf("%d", knapSack(W, wt, val, n));

return 0;

You might also like