DAA File
DAA File
CSP350
S. Page.N Rema
Lab Experiment Contents Date Signature
No o rk
#include <stdio.h>
int a1[n];
int key;
printf("Enter the key to search: ");
scanf("%d", &key);
1
Output:
2
Basic Program : 2
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;
6
Program:
int main() {
int m, n;
int nums1[m];
int nums2[n];
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;
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>
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:
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);
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:
Programming Language: C
Program:
#include <stdio.h>
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);
return 0;
}
Output:-
18
EXPERIMENT 7
AIM: Writing a program to perform LCS.
Programming Language: C
Program:
#include <stdio.h>
#include <string.h>
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
Programming Language: C
Program:
#include <stdio.h>
#include <limits.h>
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
Programming Language: C
Program:
#include <stdio.h>
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
Programming Language: C
Program:
#include <stdio.h>
#include <limits.h>
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.
Programming Language: C
Program:
#include <stdio.h>
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
Programming Language: C
Program:
#include <stdio.h>
#include <stdlib.h>
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.
Programming Language: C
Program:
#include <stdio.h>
#define INF 99999
#define V 4
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
Programming Language: C
Program:
#include <stdio.h>
#include <string.h>
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.
Programming Language: C
Program:
#include <stdio.h>
#include <string.h>
#define d 256
#define q 101
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.
Programming Language: C
Program:
#include <stdio.h>
#include <string.h>
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.
Programming Language: C
Program:
#include <stdio.h>
#include <stdlib.h>
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