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

Daa Lab Program

DAA LAB PROGRAM

Uploaded by

bansallkoup32
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)
14 views

Daa Lab Program

DAA LAB PROGRAM

Uploaded by

bansallkoup32
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/ 9

1. Program for Recursive Binary & Linear Search.

#include <stdio.h>

int binarySearch(int arr[], int low, int high, int target) {


if (high >= low) {
int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid;

if (arr[mid] > target)


return binarySearch(arr, low, mid - 1, target);

return binarySearch(arr, mid + 1, high, target);


}

return -1;
}

int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
int result = binarySearch(arr, 0, n - 1, target);
(result == -1) ? prin ("Element not present\n") : prin ("Element found at index %d\n",
result);
return 0;
}
2. Program for Heap Sort.
#include <stdio.h>

void heapify(int arr[], int n, int i) {


int largest = i;
int le = 2 * i + 1;
int right = 2 * i + 2;

if (le < n && arr[le ] > arr[largest])


largest = le ;

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);
}}
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[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
prin ("%d ", arr[i]);
prin ("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, n);
prin ("Sorted array: ");
printArray(arr, n);
}
3. Program for Merge Sort.
#include <stdio.h>

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];

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


L[i] = arr[l + i];
for (int i = 0; i < n2; i++)
R[i] = arr[m + 1 + i];

int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[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++)
prin ("%d ", arr[i]);
prin ("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, arr_size - 1);


prin ("Sorted array: ");
printArray(arr, arr_size);
}
4. Program for Selec on Sort.
#include <stdio.h>

void selec onSort(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;

int temp = arr[min_idx];


arr[min_idx] = arr[i];
arr[i] = temp;}}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
prin ("%d ", arr[i]);
prin ("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selec onSort(arr, n);
prin ("Sorted array: ");
printArray(arr, n);
}
5. Program for Inser on Sort.
#include <stdio.h>

void inser onSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
prin ("%d ", arr[i]);
prin ("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
inser onSort(arr, n);
prin ("Sorted array: ");
printArray(arr, n);
}
6. Program for Quick Sort.
#include <stdio.h>

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


int t = *a;
*a = *b;
*b = t;
}
int par on(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

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


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = par on(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
prin ("%d ", arr[i]);
prin ("\n");
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
prin ("Sorted array: ");
printArray(arr, n);
}
7. Knapsack Problem using Greedy Solu on
#include <stdio.h>

void knapsackGreedy(int n, float weight[], float profit[], float capacity) {


float x[n], totalProfit = 0;
int i, j;
for (i = 0; i < n; i++)
x[i] = 0.0;

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


if (weight[i] <= capacity) {
x[i] = 1.0;
capacity -= weight[i];
totalProfit += profit[i];
} else {
x[i] = capacity / weight[i];
totalProfit += x[i] * profit[i];
break;
}
}

prin ("Total Profit = %f\n", totalProfit);


}
int main() {
int n = 3;
float weight[] = {10, 20, 30};
float profit[] = {60, 100, 120};
float capacity = 50;
knapsackGreedy(n, weight, profit, capacity);
}
8. Perform Travelling Salesman Problem.
#include <stdio.h>
#include <limits.h>

#define V 4

int travellingSalesman(int graph[][V], int pos, int visited, int memo[][1 << V]) {
if (visited == (1 << V) - 1)
return graph[pos][0];

if (memo[pos][visited] != -1)
return memo[pos][visited];

int ans = INT_MAX;

for (int city = 0; city < V; city++) {


if (!(visited & (1 << city))) {
int newAns = graph[pos][city] + travellingSalesman(graph, city, visited | (1 << city),
memo);
ans = newAns < ans ? newAns : ans;
}
}

return memo[pos][visited] = ans;


}

int main() {
int graph[][V] = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}};

int memo[V][1 << V];


for (int i = 0; i < V; i++)
for (int j = 0; j < (1 << V); j++)
memo[i][j] = -1;

prin ("Minimum cost: %d\n", travellingSalesman(graph, 0, 1, memo));


return 0;
}
9. Find Minimum Spanning Tree using Kruskal’s Algorithm.
#include <stdio.h>
#include <stdlib.h>

struct Edge {
int src, dest, weight;
};

struct Graph {
int V, E;
struct Edge* edge;
};

struct Graph* createGraph(int V, int E) {


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*)malloc(graph->E * sizeof(struct Edge));
return graph;
}

struct subset {
int parent;
int rank;
};
int find(struct subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(struct subset subsets[], int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)


subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int cmp(const void* a, const void* b) {
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}

void KruskalMST(struct Graph* graph) {


int V = graph->V;
struct Edge result[V];
int e = 0;
int i = 0;

qsort(graph->edge, graph->E, sizeof(graph->edge[0]), cmp);

struct subset* subsets = (struct subset*)malloc(V * sizeof(struct subset));

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


subsets[v].parent = v;
subsets[v].rank = 0;
}

while (e < V - 1 && i < graph->E) {


struct Edge next_edge = graph->edge[i++];

int x = find(subsets, next_edge.src);


int y = find(subsets, next_edge.dest);

if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}

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


for (i = 0; i < e; ++i)
prin ("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);
return;
}

int main() {
int V = 4;
int E = 5;
struct Graph* graph = createGraph(V, E);

graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;

graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;

graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;

graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;

KruskalMST(graph);

return 0;
}

You might also like