0% found this document useful (0 votes)
28 views36 pages

DAA File

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

DAA File

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

DESIGN & ANALYSIS OF ALGORITHMS LAB

CSP350

B.TECH 3RD YEAR


SEMESTER: 5th
SESSION: 2024-2025

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SHARDA UNIVERSITY, GREATER NOIDA
Submitted By: Submitted to:

Name: Hani Kumar Mr. Jitendra Singh


Roll No / System ID: 2201010259/2022431320
Section/Group: P/G2
Table of Content

S. Page.N Rema
Lab Experiment Contents Date Signature
No o rk

Writing a program to demonstrate


1. Basic Program 1
the concept of Linear search.

Writing a program to implement


2. Basic Program 2
Binary Search

Writing a program to find the median


3. Experiment 1 of the sorted array, the result of
merging of two sorted arrays.

Writing a program to arrange


4. Experiment 2 the library books in alphabetical
order using Quicksort.

Writing a program to arrange the


5. Experiment 3 stop values in ascending order
using merge sort.

Writing a program to sort the array


6. Experiment 4 of integer in ascending order
using counting sort.

Write a program to arrange the


7. Experiment 5 weight of people in descending order
using bucket sort.
Write a program to arrange the
8. Experiment 6 weight of people in descending order
using bucket sort.

9. Experiment 7 Writing a program to implement lcs.

Writing a program to implement


10 Experiment 8
optimal Matrix chain multiplication.

Write a program to implement 0/1


11 Experiment 9
knapsack problems.
Basic Program : 1
Aim: Implementing the Linear Search.
Course Outcome : CO1
Software Tool: VS code
Programming Language: C
Program:

#include <stdio.h>

int linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int a1[n];

printf("Enter the elements of the array:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &a1[i]);
}

int key;
printf("Enter the key to search: ");
scanf("%d", &key);

int index = linearSearch(a1, n, key);


if (index != -1) {
printf("%d is found at index: %d\n", key, index);
} else {
printf("%d is not found in the array.\n", key);
}

printf("Written By: Hani kumar / 2022431320\n");


return 0;
}

1
Output:

2
Basic Program : 2

Aim: Implementing the Binary Search.


Course Outcome : CO1
Software Tool: VS code
Programming Language: C
Program:
#include <stdio.h>
int binarySearch(int array[], int n, int target) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int numbers[n];
printf("Enter the elements of the array (in sorted order):\n");
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
int target;
printf("Enter the target element to search: ");
scanf("%d", &target);
int result = binarySearch(numbers, n, target);
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found in the array.\n"); }
printf("Written By: Hani kumar / 2022431320\n");
return 0;
}

3
Output:

4
5
Experiment : 1

Aim: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the
median of the two sorted arrays.
Course Outcome :
CO1

Software
#include Tool: VS
<stdio.h>
#include <limits.h>
code Programming Language:
C double findMedianSortedArrays(int nums1[], int m, int nums2[],
int n) {
if (m > n) {
return findMedianSortedArrays(nums2, n, nums1, m);
}

int totalLength = m + n;
int half = (totalLength + 1) / 2;

int left = 0, right = m;

while (left <= right) {


int i = (left + right) / 2;
int j = half - i;

int nums1Left = (i == 0) ? INT_MIN : nums1[i - 1];


int nums1Right = (i == m) ? INT_MAX : nums1[i];
int nums2Left = (j == 0) ? INT_MIN : nums2[j - 1];
int nums2Right = (j == n) ? INT_MAX : nums2[j];

if (nums1Left <= nums2Right && nums2Left <=


nums1Right) {
if (totalLength % 2 == 0) {
return (fmax(nums1Left, nums2Left) +
fmin(nums1Right, nums2Right)) / 2.0;
} else {
return fmax(nums1Left, nums2Left);
}
} else if (nums1Left > nums2Right) {
right = i - 1;
} else {
left = i + 1;
}

6
Program:

int main() {
int m, n;

printf("Enter the number of elements in the first array: ");


scanf("%d", &m);

int nums1[m];

printf("Enter the elements of the first array (in sorted order):\n");


for (int i = 0; i < m; i++) {
scanf("%d", &nums1[i]);
}

printf("Enter the number of elements in the second array: ");


scanf("%d", &n);

int nums2[n];

printf("Enter the elements of the second array (in sorted order):\n");


for (int i = 0; i < n; i++) {
scanf("%d", &nums2[i]);
}

double median = findMedianSortedArrays(nums1, m, nums2, n);


printf("The median is: %.1f\n", median);
printf("Written By: Hani kumar / 2022431320n");

return 0;
}

7
Output:

8
Experiment : 2

Aim: Consider a library that has received 1 million books and tomorrow is the inauguration
ceremony of the library. All the books are required to be arranged alphabetically in racks
before the inauguration. The task has to be completed in the least possible time. Using a
quick sort algorithm, implement the situation.
Course Outcome :
CO1 Software Tool: VS code
Programming Language: C
Program:

#include <stdio.h>
#include <string.h>
void quickSort(char *arr[], int low, int
high);
int partition(char *arr[], int low, int high);
int main() {
int n;
printf("Enter the number of books: ");
scanf("%d", &n);
getchar();
char *books[n];
printf("Enter the titles of the books:\n");
for (int i = 0; i < n; i++) {
books[i] = malloc(100 * sizeof(char));
fgets(books[i], 100, stdin);
books[i][strcspn(books[i], "\n")] = '\0';
}
quickSort(books, 0, n - 1);
printf("Sorted books:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", books[i]);
free(books[i]);
}
printf("Written By: Hani kumar /
2022431320 \n");
return 0;
}
void quickSort(char *arr[], int low, int

9
int partition(char *arr[], int low, int high) {
char *pivot = arr[high];
int i = low - 1;

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


if (strcmp(arr[j], pivot) < 0) {
i++;
// Swap arr[i] and arr[j]
char *temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// Swap arr[i + 1] and arr[high]


char *temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

return i + 1;
}

Output:

10
Experiment : 3
Aim: Writing a program to arrange the stop values in ascending order using merge sort.
Course Outcome : CO1
Software Tool: VS code
Programming Language: C
Program:

#include <stdio.h>
#include <stdlib.h>

void merge(int arr[], int left, int mid, int right)


{
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int 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++;
}
free(L);

11
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("\nUnsorted array: ");
for (int i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
printf("\n");
mergeSort(arr, 0, arr_size - 1);
printf("Sorted array: ");
for (int i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
printf("\n\n Hani kumar / 2022431320\n");
return 0;
}

Output:

12
Experiment : 4

Aim: Writing a program to sort the array of integers in ascending order using counting sort.
Course Outcome:
CO1 Software Tool: VS code
Programming Language: C
Program:
#include <stdio.h>
#include <stdlib.h>
int findMax(int array[], int size) {
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
void countingSort(int array[], int size) {
int max = findMax(array, size);
int *count = (int *)malloc((max + 1) * sizeof(int));
for (int i = 0; i <= max; i++) {
count[i] = 0;
}
for (int i = 0; i < size; i++) {
count[array[i]]++;
}
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
int *output = (int *)malloc(size * sizeof(int));
for (int i = size - 1; i >= 0; i--) {
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for (int i = 0; i < size; i++) {
array[i] = output[i];
}
free(count);
free(output);
}
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

13
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int *array = (int *)malloc(size * sizeof(int));
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Original array:\n");
printArray(array, size);
countingSort(array, size);
printf("Sorted array:\n");
printArray(array, size);
free(array);
printf("Name:- Hani kumar / 2022431320 ");

return 0;
}

Output:-

14
EXPERIMENT 5
AIM: Write a program to arrange the weight of people in descending order using bucket sort Software tool used:

Course Outcome: CO1

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

struct Node {
float data;
struct Node* next;
};
struct Node* createNode(float data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void sortedInsert(struct Node** bucket, float data) {
struct Node* newNode = createNode(data);
if (*bucket == NULL || (*bucket)->data < data) {
newNode->next = *bucket;
*bucket = newNode;
} else {
struct Node* current = *bucket;
while (current->next != NULL && current->next->data >= data) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
void bucketSort(float arr[], int n) {
struct Node* buckets[MAX] = {NULL};
for (int i = 0; i < n; i++) {
int index = (int)arr[i];
sortedInsert(&buckets[index], arr[i]);
}
int j = 0;
for (int i = MAX - 1; i >= 0; i--) {
struct Node* node = buckets[i];
while (node != NULL) {
arr[j++] = node->data;
struct Node* temp = node;
node = node->next;
free(temp);

15
int main() {
int n;
printf("Enter the number of people: ");
scanf("%d", &n);

float weights[n];
printf("Enter the weights of people:\n");
for (int i = 0; i < n; i++) {
scanf("%f", &weights[i]);
}
bucketSort(weights, n);

printf("Weights in descending order:\n");


for (int i = 0; i < n; i++) {
printf("%.2f ", weights[i]);
}
printf("\n");

printf("Hani kumar / 2022431320\n");

return 0;
}

Output:-

16
EXPERIMENT 6
AIM: Write a program to arrange the weight of people in descending order using bucket sort Software tool used:

Course Outcome: CO1

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>

int getMax(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

void countSort(int arr[], int n, int exp) {


int output[n], count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}

void radixSort(int arr[], int n) {


int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}

17
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

radixSort(arr, n);

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


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

printf("\nSorted elements in descending order:\n");


for (int i = n - 1; i >= 0; i--)
printf("%d ", arr[i]);

printf("\n Hani kumar / 2022431320\n");

return 0;
}

Output:-

18
EXPERIMENT 7
AIM: Writing a program to perform LCS.

Course Outcome: CO1

Software Tool: VS code

Programming Language: C

Program:

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

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

int lcs(char *X, char *Y, int m, int n) {


int L[m + 1][n + 1];
for (int i = 0; i <= m; i++)
for (int j = 0; j <= n; j++)
L[i][j] = (i == 0 || j == 0) ? 0 : (X[i - 1] == Y[j - 1] ? L[i - 1][j - 1] + 1 : max(L[i - 1][j],
L[i][j - 1]));
return L[m][n];
}

int main() {
char X[100], Y[100];
printf("Enter two strings: ");
scanf("%s %s", X, Y);
printf("LCS length: %d\n", lcs(X, Y, strlen(X), strlen(Y)));
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

19
EXPERIMENT 8
AIM: Writing a program to implement optimal Matrix chain multiplication

Course Outcome: CO1

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <limits.h>

int MatrixChain(int p[], int n) {


int m[n][n];
for (int i = 1; i < n; i++) m[i][i] = 0;
for (int len = 2; len < n; len++)
for (int i = 1; i < n - len + 1; i++) {
int j = i + len - 1;
m[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++)
m[i][j] = m[i][j] < m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j] ? m[i][j] : m[i][k]
+ m[k + 1][j] + p[i - 1] * p[k] * p[j];
}
return m[1][n - 1];
}

int main() {
int n, p[100];
printf("Enter number of matrices: ");
scanf("%d", &n);
printf("Enter dimensions: ");
for (int i = 0; i <= n; i++) scanf("%d", &p[i]);
printf("Minimum multiplications: %d\n", MatrixChain(p, n + 1));
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

20
EXPERIMENT 9
AIM: Write a program to implement 0/1 knapsack problem using Greedy Approach

Course Outcome: CO1

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>

int knapsack(int W, int wt[], int val[], int n) {


int total = 0;
for (int i = 0; i < n; i++)
if (W >= wt[i]) W -= wt[i], total += val[i];
return total;
}

int main() {
int n, W, wt[100], val[100];
printf("Enter number of items and capacity: ");
scanf("%d %d", &n, &W);
printf("Enter weights and values: ");
for (int i = 0; i < n; i++) scanf("%d %d", &wt[i], &val[i]);
printf("Maximum value: %d\n", knapsack(W, wt, val, n));
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

21
EXPERIMENT 10
AIM: Write a program to implement fractional Knapsack problems using a greedy approach.
Course Outcome: CO1
Software Tool: VS code
Programming Language: C
Program:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int weight, value;
double ratio;
} Item;
int cmp(const void *a, const void *b) {
return ((Item*)b)->ratio > ((Item*)a)->ratio ? 1 : -1;
}
double fractionalKnapsack(int W, Item arr[], int n) {
qsort(arr, n, sizeof(Item), cmp);
double total = 0;
for (int i = 0; i < n && W > 0; i++) {
int wt = arr[i].weight < W ? arr[i].weight : W;
total += wt * arr[i].ratio;
W -= wt;
}
return total;
}

int main() {
int n, W;
Item arr[100];
printf("Enter number of items and capacity: ");
scanf("%d %d", &n, &W);
printf("Enter weights and values: ");
for (int i = 0; i < n; i++) {
scanf("%d %d", &arr[i].weight, &arr[i].value);
arr[i].ratio = (double)arr[i].value / arr[i].weight;
}
printf("Maximum value: %.2f\n", fractionalKnapsack(W, arr, n));
printf("Hani kumar / 2022431320\n");
return 0;
}

22
Output:-

23
EXPERIMENT 11
AIM: From a given vertex in a weighted connected graph, find shortest paths to other vertices using Dijkstra's
algorithm
Course Outcome: CO2

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <limits.h>

int minDistance(int dist[], int sptSet[], int V) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}

void dijkstra(int graph[100][100], int src, int V) {


int dist[V], sptSet[V];
for (int i = 0; i < V; i++) dist[i] = INT_MAX, sptSet[i] = 0;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet, V);
sptSet[u] = 1;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] <
dist[v])
dist[v] = dist[u] + graph[u][v];
}
for (int i = 0; i < V; i++)
printf("Distance to %d: %d\n", i, dist[i]);
}

int main() {
int V, graph[100][100], src;
printf("Enter number of vertices and source vertex: ");
scanf("%d %d", &V, &src);
printf("Enter graph matrix:\n");
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
scanf("%d", &graph[i][j]);
dijkstra(graph, src, V);
printf("Hani kumar / 2022431320\n");
return 0;
}

24
Output:-

25
EXPERIMENT 12
AIM: Write a program to implement the Sum of Subset problem.

Course Outcome: CO4

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>

int subsetSum(int set[], int n, int sum) {


if (sum == 0) return 1;
if (n == 0) return 0;
return subsetSum(set, n - 1, sum) || subsetSum(set, n - 1, sum - set[n - 1]);
}

int main() {
int n, sum, set[100];
printf("Enter number of elements and target sum: ");
scanf("%d %d", &n, &sum);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &set[i]);
printf("Subset with given sum %s\n", subsetSum(set, n, sum) ? "exists" : "does not
exist");
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

26
EXPERIMENT 13
AIM: WAP to demonstrate the concept of Red Black Tree insertion and Deletion.
Course Outcome: CO4

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
char color;
struct Node *left, *right, *parent;
} Node;

Node *root = NULL;

Node *createNode(int data) {


Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->color = 'R';
newNode->left = newNode->right = newNode->parent = NULL;
return newNode;
}

void leftRotate(Node *x) {


Node *y = x->right;
x->right = y->left;
if (y->left) y->left->parent = x;
y->parent = x->parent;
if (!x->parent) root = y;
else if (x == x->parent->left) x->parent->left = y;
else x->parent->right = y;
y->left = x;
x->parent = y;
}

void rightRotate(Node *y) {


Node *x = y->left;
y->left = x->right;
if (x->right) x->right->parent = y;
x->parent = y->parent;
if (!y->parent) root = x;
else if (y == y->parent->right) y->parent->right = x;
else y->parent->left = x;
x->right = y;
y->parent = x;
}

27
void fixInsert(Node *z) {
while (z != root && z->parent->color == 'R') {
if (z->parent == z->parent->parent->left) {
Node *y = z->parent->parent->right;
if (y && y->color == 'R') {
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
leftRotate(z);
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
rightRotate(z->parent->parent);
}
} else {
Node *y = z->parent->parent->left;
if (y && y->color == 'R') {
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
rightRotate(z);
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
leftRotate(z->parent->parent);
} } }
root->color = 'B';
}
void insert(int data) {
Node *z = createNode(data), *y = NULL, *x = root;
while (x) {
y = x;
if (z->data < x->data) x = x->left;
else x = x->right;
}
z->parent = y;
if (!y) root = z;
else if (z->data < y->data) y->left = z;
else y->right = z;
fixInsert(z);
}

28
void inorder(Node *node) {
if (node) {
inorder(node->left);
printf("%d %c\n", node->data, node->color);
inorder(node->right);
}
}

int main() {
insert(10);
insert(20);
insert(30);
insert(15);
inorder(root);
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

29
EXPERIMENT 14
AIM: Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.

Course Outcome: CO4

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#define INF 99999
#define V 4

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


int dist[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (int k = 0; k < V; k++)
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
if (dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF) printf("%7s", "INF");
else printf("%7d", dist[i][j]);
}
printf("\n");
}
}

int main() {
int graph[V][V] = {{0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0}};
floydWarshall(graph);
printf("Hani kumar / 2022431320\n");
return 0;
}
Output:

30
EXPERIMENT 15
AIM: Write a program to implement Naïve string matching problem

Course Outcome: CO4

Software Tool: VS code

Programming Language: C

Program:

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

void naiveSearch(char *pat, char *txt) {


int M = strlen(pat);
int N = strlen(txt);
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M)
printf("Pattern found at index %d\n", i);
}
}

int main() {
char txt[] = "ABAAABCD";
char pat[] = "ABC";
naiveSearch(pat, txt);
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

31
EXPERIMENT 16
AIM: Write a program to implement the Rabin Karp Algorithm problem.

Course Outcome: CO4

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <string.h>
#define d 256
#define q 101

void rabinKarp(char *pat, char *txt) {


int M = strlen(pat);
int N = strlen(txt);
int i, j, p = 0, t = 0, h = 1;
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
for (i = 0; i < M; i++) {
p = (d * p + pat[i]) % q;
t = (d * t + txt[i]) % q;
}
for (i = 0; i <= N - M; i++) {
if (p == t) {
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M)
printf("Pattern found at index %d\n", i); }
if (i < N - M) {
t = (d * (t - txt[i] * h) + txt[i + M]) % q;
if (t < 0) t = (t + q);
}}}
int main() {
char txt[] = "GEEKS FOR GEEKS";
char pat[] = "GEEK";
rabinKarp(pat, txt);
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

32
EXPERIMENT 17
AIM: You are given a string of 2N characters consisting of N '[' brackets and N ']' brackets. A string is considered
balanced if it can be represented in the for S2[S1] where S1 and S2 are balanced strings. We can make an
unbalanced string balanced by swapping adjacent characters. Calculate the minimum number of swaps necessary to
make a string balanced.

Course Outcome: CO5

Software Tool: VS code

Programming Language: C

Program:

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

int minSwaps(char *s) {


int balance = 0, swaps = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == '[') balance++;
else balance--;
if (balance < 0) {
swaps++;
balance = 1;
}
}
return swaps;
}

int main() {
char s[] = "[]][][";
printf("Minimum swaps: %d\n", minSwaps(s));
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

33
EXPERIMENT 18
AIM: There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A
mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x -1. Any of
these moves consumes 1 minute. Assign mice to holes so that the time when the last mouse gets inside a hole is
minimized.

Course Outcome: CO5

Software Tool: VS code

Programming Language: C

Program:

#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

int minTime(int mice[], int holes[], int n) {


qsort(mice, n, sizeof(int), compare);
qsort(holes, n, sizeof(int), compare);
int maxTime = 0;
for (int i = 0; i < n; i++)
if (abs(mice[i] - holes[i]) > maxTime)
maxTime = abs(mice[i] - holes[i]);
return maxTime;
}

int main() {
int mice[] = {4, -4, 2};
int holes[] = {4, 0, 5};
int n = sizeof(mice) / sizeof(mice[0]);
printf("Minimum time: %d\n", minTime(mice, holes, n));
printf("Hani kumar / 2022431320\n");
return 0;
}

Output:-

34

You might also like