Daa PR
Daa PR
Daa PR
Quick Sort
#include <stdio.h>
int main() {
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
quickSort(arr, 0, n - 1);
return 0;
}
2. Merge Sort
#include <stdio.h>
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
int main() {
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
3. Binary Search
#include <stdio.h>
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main() {
int arr[] = { 11, 12, 22, 25, 64 };
int n = sizeof(arr) / sizeof(arr[0]);
int target = 22;
if (result == -1)
printf("Element not found\n");
else
printf("Element found at index %d\n", result);
return 0;
}
4. Min- Max
#include <stdio.h>
int main() {
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
int min, max;
return 0;
}
5. Knapsack
#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
if (weights[n - 1] > W)
return knapsack(W, weights, values, n - 1);
return max(
values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1),
knapsack(W, weights, values, n - 1)
);
}
int main() {
int weights[] = { 10, 20, 30 };
int values[] = { 60, 100, 120 };
int W = 50;
int n = sizeof(values) / sizeof(values[0]);
return 0;
}
6. Dijkstra
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define V 9
return minIndex;
}
dist[source] = 0;
printSolution(dist);
}
int main() {
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
7. Kruskals
#include <stdio.h>
#include <stdbool.h>
#define V 6
#define E 8
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
struct Edge edges[E];
};
graph->edges[0].src = 0;
graph->edges[0].dest = 1;
graph->edges[0].weight = 4;
graph->edges[1].src = 0;
graph->edges[1].dest = 2;
graph->edges[1].weight = 4;
graph->edges[2].src = 1;
graph->edges[2].dest = 2;
graph->edges[2].weight = 2;
graph->edges[3].src = 2;
graph->edges[3].dest = 3;
graph->edges[3].weight = 3;
graph->edges[4].src = 2;
graph->edges[4].dest = 5;
graph->edges[4].weight = 2;
graph->edges[5].src = 2;
graph->edges[5].dest = 4;
graph->edges[5].weight = 4;
graph->edges[6].src = 3;
graph->edges[6].dest = 4;
graph->edges[6].weight = 3;
graph->edges[7].src = 5;
graph->edges[7].dest = 4;
graph->edges[7].weight = 3;
return graph;
}
int e = 0;
int i = 0;
if (x != y) {
result[e++] = next_edge;
Union(parent, rank, x, y);
}
}
int main() {
struct Graph* graph = createGraph();
kruskal(graph);
return 0;
}
8. Prims
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define V 5
return minIndex;
}
key[0] = 0;
parent[0] = -1;
printMST(parent, 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;
}
int main() {
int set[] = { 3, 34, 4, 12, 5, 2 };
int sum = 9;
int n = sizeof(set) / sizeof(set[0]);
if (isSubsetSum(set, n, sum))
printf("Found a subset with given sum\n");
else
printf("No subset with given sum\n");
return 0;
}
if (j == M)
printf("Pattern found at index %d\n", i);
}
}
int main() {
char text[] = "AABAACAADAABAAABAA";
char pattern[] = "AABA";
naiveStringMatch(text, pattern);
return 0;
}