0% found this document useful (0 votes)
9 views23 pages

23BCE0421

The document contains lab assignments for the course BCSE204P, focusing on algorithm design and analysis. It includes code implementations for finding the kth smallest and largest elements in a list, searching for an element using recursive methods, and solving problems using greedy and divide-and-conquer approaches. Specific algorithms discussed include Dijkstra’s algorithm, fractional knapsack, merge sort, and Karatsuba multiplication.

Uploaded by

kavinvit13
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)
9 views23 pages

23BCE0421

The document contains lab assignments for the course BCSE204P, focusing on algorithm design and analysis. It includes code implementations for finding the kth smallest and largest elements in a list, searching for an element using recursive methods, and solving problems using greedy and divide-and-conquer approaches. Specific algorithms discussed include Dijkstra’s algorithm, fractional knapsack, merge sort, and Karatsuba multiplication.

Uploaded by

kavinvit13
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/ 23

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

Lab Assignment - I, Winter Semester 2024-25

Course Code: BCSE204P


Design and Analysis of Algorithms
Slot: L43 + L54

Name: EK.KAVINKUMAR
Reg. No: 23BCE0421

Faculty: Dr. Srivani A


Q1) To find the kth smallest and kth largest element in a list of
elements without sorting the elements.

Code:

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

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++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i + 1];


arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

int quickselect(int arr[], int low, int high, int k) {


if (low <= high) {
int pi = partition(arr, low, high);

if (pi == k)
return arr[pi];
else if (pi < k)
return quickselect(arr, pi + 1, high, k);
else
return quickselect(arr, low, pi - 1, k);
}

return -1;
}

int kthLargest(int arr[], int n, int k) {


return quickselect(arr, 0, n - 1, n - k);
}

int main() {
int n, k;

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


scanf("%d", &n);

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

printf("Enter the value of k: ");


scanf("%d", &k);

if (k > 0 && k <= n) {


int kthSmallest = quickselect(arr, 0, n - 1, k - 1);
printf("The %dth smallest element is: %d\n", k, kthSmallest);

int kthLargestElement = kthLargest(arr, n, k);


printf("The %dth largest element is: %d\n", k,
kthLargestElement);
} else {
printf("Invalid k. Please enter a value between 1 and %d.\n", n);
}
return 0;
}

Output:

Q2) To search for an element using recursive linear or binary search


based on the option given by the user. The number of comparisons
taken to find the element also must be found.

Code:
#include <stdio.h>

int comparisons = 0;

int linearSearch(int arr[], int size, int target, int index) {


comparisons++;
if (index >= size) {
return -1;
}
if (arr[index] == target) {
return index;
}
return linearSearch(arr, size, target, index + 1);
}

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


comparisons++;
if (low > high) {
return -1;
}
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
return binarySearch(arr, low, mid - 1, target);
} else {
return binarySearch(arr, mid + 1, high, target);
}
}
int main() {
int n, target, option;

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


scanf("%d", &n);

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

printf("Enter the value to search for: ");


scanf("%d", &target);

printf("Choose search method:\n1. Recursive Linear Search\n2.


Recursive Binary Search\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &option);

int result = -1;


comparisons = 0;

switch(option) {
case 1:
result = linearSearch(arr, n, target, 0);
break;
case 2:
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
result = binarySearch(arr, 0, n - 1, target);
break;
default:
printf("Invalid choice!\n");
return -1;
}

if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}

printf("Number of comparisons: %d\n", comparisons);

return 0;
}

Output:
Q3) Greedy Approach :
i) Dijkstra’s Single Source Shortest Path Algorithm
ii) Fractional Knapsack Problem for an airport
permitted baggage scenario.

Code i):
#include <stdio.h>
#include <limits.h>

#define V 9

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


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[V][V], int src) {
int dist[V];
int 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);
sptSet[u] = 1;

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


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

printf("Vertex\tDistance from Source\n");


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

int main() {
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 0, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 0},
{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, 0, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 0, 0, 0, 0, 6, 7, 0}
};

int source = 0;
dijkstra(graph, source);

return 0;
}
Output i):

Code ii):

#include <stdio.h>

typedef struct {
int weight;
int value;
float ratio;
} Item;

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


Item temp = *a;
*a = *b;
*b = temp;
}

void sortByRatio(Item arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].ratio < arr[j + 1].ratio) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}

float fractionalKnapsack(int W, Item items[], int n) {


sortByRatio(items, n);
float totalValue = 0.0;
for (int i = 0; i < n; i++) {
if (W == 0) {
break;
}
if (items[i].weight <= W) {
totalValue += items[i].value;
W -= items[i].weight;
} else {
totalValue += items[i].value * ((float)W / items[i].weight);
break;
}
}
return totalValue;
}

int main() {
int n, W;

printf("Enter the number of items: ");


scanf("%d", &n);

Item items[n];

printf("Enter the weight and value of each item:\n");


for (int i = 0; i < n; i++) {
scanf("%d %d", &items[i].weight, &items[i].value);
items[i].ratio = (float)items[i].value / items[i].weight;
}

printf("Enter the capacity of the knapsack: ");


scanf("%d", &W);
float maxValue = fractionalKnapsack(W, items, n);
printf("Maximum value in Knapsack = %.2f\n", maxValue);

return 0;
}

Output ii):

Q4) Divide and Conquer Approach:


i) Quick Sort or Merge Sort to sort the details of
mobile phones based on their RAM capacity
ii) Karatsuba Faster Integer Multiplication Problem.
Code i):
#include <stdio.h>
#include <string.h>

typedef struct {
char model[50];
int ram;
} MobilePhone;

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


int n1 = mid - left + 1;
int n2 = right - mid;

MobilePhone L[n1], R[n2];

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].ram <= R[j].ram) {
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(MobilePhone 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);


}
}

void printMobilePhones(MobilePhone arr[], int n) {


for (int i = 0; i < n; i++) {
printf("Model: %s, RAM: %d GB\n", arr[i].model, arr[i].ram);
}
}

int main() {
int n;

printf("Enter the number of mobile phones: ");


scanf("%d", &n);

MobilePhone arr[n];
printf("Enter the details (Model and RAM capacity) of each mobile
phone:\n");
for (int i = 0; i < n; i++) {
printf("Model %d: ", i + 1);
scanf("%s %d", arr[i].model, &arr[i].ram);
}

mergeSort(arr, 0, n - 1);

printf("\nSorted Mobile Phones by RAM capacity:\n");


printMobilePhones(arr, n);

return 0;
}

Output i) :
Code ii):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

long long karatsuba(long long x, long long y) {


if (x < 10 || y < 10)
return x * y;

int n = (int)log10(x) + 1;
int n2 = n / 2;

long long a = x / (long long)pow(10, n2);


long long b = x % (long long)pow(10, n2);
long long c = y / (long long)pow(10, n2);
long long d = y % (long long)pow(10, n2);

long long ac = karatsuba(a, c);


long long bd = karatsuba(b, d);
long long ad_plus_bc = karatsuba(a + b, c + d) - ac - bd;

return ac * (long long)pow(10, 2 * n2) + ad_plus_bc * (long


long)pow(10, n2) + bd;
}

int main() {
long long x, y;
printf("Enter two integers: ");
scanf("%lld %lld", &x, &y);

long long product = karatsuba(x, y);


printf("Product: %lld\n", product);

return 0;
}
Output ii):

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy