Daa PR

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

1.

Quick Sort
#include <stdio.h>

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


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

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


int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; 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 pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}

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


for (int 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]);
printf("Original array: ");
printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}

2. Merge Sort
#include <stdio.h>

void merge(int arr[], int left, int middle, int right) {


int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;

int L[n1], R[n2];

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


L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[middle + 1 + j];

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

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int middle = left + (right - left) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
}

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


for (int 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]);

printf("Original array: ");


printArray(arr, n);

mergeSort(arr, 0, n - 1);

printf("Sorted array: ");


printArray(arr, n);
return 0;
}

3. Binary Search
#include <stdio.h>

int binarySearch(int arr[], int left, int right, int target) {


while (left <= right) {
int mid = left + (right - left) / 2;

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;

int result = binarySearch(arr, 0, n - 1, target);

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>

void findMinMax(int arr[], int size, int* min, int* max) {


*min = arr[0];
*max = arr[0];

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


if (arr[i] < *min)
*min = arr[i];
if (arr[i] > *max)
*max = arr[i];
}
}

int main() {
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
int min, max;

findMinMax(arr, n, &min, &max);

printf("Minimum value: %d\n", min);


printf("Maximum value: %d\n", max);

return 0;
}

5. Knapsack
#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}

int knapsack(int W, int weights[], int values[], int n) {


if (n == 0 || W == 0)
return 0;

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

int maxVal = knapsack(W, weights, values, n);

printf("Maximum value: %d\n", maxVal);

return 0;
}

6. Dijkstra
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>

#define V 9

int minDistance(int dist[], bool visited[]) {


int min = INT_MAX;
int minIndex;

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


if (!visited[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}

return minIndex;
}

void printSolution(int dist[]) {


printf("Vertex \t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[V][V], int source) {


int dist[V];
bool visited[V];

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


dist[i] = INT_MAX;
visited[i] = false;
}

dist[source] = 0;

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


int u = minDistance(dist, visited);
visited[u] = true;

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


if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] <
dist[v])
dist[v] = dist[u] + graph[u][v];
}
}

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

struct Graph* createGraph() {


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->E = 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 find(int parent[], int i) {


if (parent[i] == i)
return i;
return find(parent, parent[i]);
}

void Union(int parent[], int rank[], int x, int y) {


int xroot = find(parent, x);
int yroot = find(parent, y);

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


parent[xroot] = yroot;
else if (rank[xroot] > rank[yroot])
parent[yroot] = xroot;
else {
parent[yroot] = xroot;
rank[xroot]++;
}
}

void kruskal(struct Graph* graph) {


int parent[V];
int rank[V];

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


parent[i] = i;
rank[i] = 0;
}

int e = 0;
int i = 0;

struct Edge result[V - 1];

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


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

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


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

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

printf("Edges in the Minimum Spanning Tree:\n");


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

int main() {
struct Graph* graph = createGraph();

kruskal(graph);

return 0;
}

8. Prims
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>

#define V 5

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


int min = INT_MAX;
int minIndex;

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


if (!mstSet[v] && key[v] < min) {
min = key[v];
minIndex = v;
}
}

return minIndex;
}

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


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

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


int parent[V];
int key[V];
bool mstSet[V];

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


key[i] = INT_MAX;
mstSet[i] = false;
}

key[0] = 0;
parent[0] = -1;

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


int u = minKey(key, mstSet);
mstSet[u] = true;

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


if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}

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

9. Sub Set Problem


#include <stdio.h>
#include <stdbool.h>

bool isSubsetSum(int set[], int n, int sum) {


if (sum == 0)
return true;
if (n == 0)
return false;

if (set[n - 1] > sum)


return isSubsetSum(set, n - 1, sum);

return isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1]);


}

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

10. String Matching Algorithm


#include <stdio.h>
#include <string.h>

void naiveStringMatch(char* text, char* pattern) {


int M = strlen(pattern);
int N = strlen(text);

for (int i = 0; i <= N - M; i++) {


int j;

for (j = 0; j < M; j++) {


if (text[i + j] != pattern[j])
break;
}

if (j == M)
printf("Pattern found at index %d\n", i);
}
}

int main() {
char text[] = "AABAACAADAABAAABAA";
char pattern[] = "AABA";

naiveStringMatch(text, pattern);

return 0;
}

You might also like