0% found this document useful (0 votes)
47 views12 pages

Ilovepdf Merged

Daa

Uploaded by

Yash Seth
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)
47 views12 pages

Ilovepdf Merged

Daa

Uploaded by

Yash Seth
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/ 12

Tanusha Choudhary

0801IT221131
Design and Analysis of Algorithms
Assignment 1

Q. Write an algorithm to rank the students according to their academic performance in


high school, higher secondary school and graduation as given in below table-1.

Normalize the obtained marks in each degree/certification in the scale of 0 to 1 of every


student, than calculate the average of these normalized marks of every student, now use
this calculated average marks of students to rank them as per their academic performance.
Input Format
Input: Take the exactly same input as given in the table-1 or you may initialize the values into
the list during the declaration.
HS_MARKS [360, 415, 280, 312, 296]
HSS_MARKS [280, 345, 247, 276, 310]
G_MARKS [6.4, 7.3, 5.9, 6.2, 8.1]
Constraints
Every decimal value should be round up to 2 decimal places.
Output Format
S2>>S5>>S1>>S4>>S3
Sample Input 0
360, 415, 280, 312, 296
280, 345, 247, 276, 310
6.4, 7.3, 5.9, 6.2, 8.1
Sample Output 0
0.23>>0.21>>0.20>>0.19>>0.17

Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>

using namespace std;


Tanusha Choudhary
0801IT221131
struct Student {
int id;
double hs_marks;
double hss_marks;
double g_marks;
double average_normalized_marks;
};

double normalize(double marks, double sum_marks) {


return marks / sum_marks;
}

bool compareStudents(const Student &a, const Student &b) {


return a.average_normalized_marks > b.average_normalized_marks;
}

int main() {
vector<Student> students = {
{1, 360, 280, 6.4},
{2, 415, 345, 7.3},
{3, 280, 247, 5.9},
{4, 312, 276, 6.2},
{5, 296, 310, 8.1}
};

double sum_hs_marks = 1663.0; // Sum of high school marks


double sum_hss_marks = 1458.0; // Sum of higher secondary school marks
double sum_g_marks = 33.9; // Sum of graduation marks

for (auto &student : students) {


double normalized_hs = normalize(student.hs_marks, sum_hs_marks);
double normalized_hss = normalize(student.hss_marks, sum_hss_marks);
double normalized_g = normalize(student.g_marks, sum_g_marks);

student.average_normalized_marks = (normalized_hs + normalized_hss + normalized_g) /


3.0;
student.average_normalized_marks = round(student.average_normalized_marks * 100) /
100.0; }

sort(students.begin(), students.end(), compareStudents);


Tanusha Choudhary
0801IT221131

for (size_t i = 0; i < students.size(); ++i) {


cout << fixed << setprecision(2) << students[i].average_normalized_marks;
if (i != students.size() - 1) {
cout << ">>";
}
}
cout << endl;

return 0;
}
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 2
Problem: Write an algorithm to search a maximum element ‘M’ from a list ‘A’. Assume the list
is already sorted in ascending/descending order, try to implement using the concept of either
linear or binary search algorithm.
Input Format
Input:First takes the size of a list as input from user and then Take the values of elements from
the user. At the end return the Maximum Value.
Sample input-0:
A [10, 20, 30, 40, 50]
Sample input-1:
A [45, 37, 29, 22, 18, 17, 13, 9, 5, 3]
Sample input-2:
A [8]
Sample input-3:
A[]
Constraints
1. ( 2 <= A <=1024 )- i.e. No. of elements in a list.
2. ( M ∈ A )- i.e. maximum element always belongs to list A.
3. Return the same Element if there is only Element in the list
Output Format
Output: Return the maximum valued element from the list.
Sample output-0: [based on the sample input-0] 50
Sample output-1: [based on the sample input-1] 45
Sample output-2: [based on the sample input-2] 8
Sample output-3: [based on the sample input-3] -1
Sample Input 0
5
10
20
30
40
50
Sample Output 0
50
Sample Input 1
1
0
Sample Output 1
0
Tanusha Choudhary
0801IT221131
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
int maxi=INT_MIN;
for(int i=0;i<n;i++){
if(a[i]>maxi) maxi=a[i];
}
cout<<maxi;
return 0;
}
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 3

Q.1 Given a list of numbers. Write an efficient algorithm to find kth smallest element from
the list of numbers without applying sorting. Value of ‘k’ is to be taken as input from the
user.

#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 - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int quickselect(int arr[], int low, int high, int k) {
if (low <= high) {
int pivotIndex = partition(arr, low, high);
if (pivotIndex == k - 1) {
return arr[pivotIndex];
} else if (pivotIndex > k - 1) {
return quickselect(arr, low, pivotIndex - 1, k);
} else {
return quickselect(arr, pivotIndex + 1, high, k);
}
}
return -1;
}
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
Tanusha Choudhary
0801IT221131
int n = sizeof(arr)/sizeof(arr[0]);
int k;
printf("Enter the value of k: ");
scanf("%d", &k);
if (k > 0 && k <= n) {
printf("The %d-th smallest element is %d\n", k, quickselect(arr, 0, n - 1, k));
} else {
printf("Invalid input for k\n");
}
return 0;
}

Q.2 Given an array consisting of total 2m elements as per the following format: { x1, x2, x3,
x4, ….., xm, y1, y2, y3, y4, …., ym }. You are required to write an efficient C program to
shuffle the array to {x1, y1, x2, y2, x3, y3, ……, xm, ym } without using extra space. (You
can use one temp variable at max for swapping operation)

#include <stdio.h>
void rotate(int arr[], int start, int mid, int end) {
int temp = arr[mid];
for (int i = mid; i > start; i--) {
arr[i] = arr[i - 1];
}
arr[start] = temp;
}
void shuffleArray(int arr[], int start, int end) {
if (end - start <= 1) return;
int mid = (start + end) / 2;
int leftMid = (start + mid) / 2;
for (int i = leftMid + 1; i <= mid; i++) {
rotate(arr, start + i - leftMid - 1, i, end);
}
shuffleArray(arr, start, mid);
shuffleArray(arr, mid + 1, end);
}
Tanusha Choudhary
0801IT221131
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);
shuffleArray(arr, 0, n - 1);
printf("Shuffled Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Q.3 Given two sorted arrays of size m and n respectively, you are required to find the
element that would be at the kth final position in the sorted array.

#include <stdio.h>
int findKth(int arr1[], int m, int arr2[], int n, int k) {
if (m > n) {
return findKth(arr2, n, arr1, m, k);
}
if (m == 0) {
return arr2[k - 1];
}
if (k == 1) {
return arr1[0] < arr2[0] ? arr1[0] : arr2[0];
}
int i = (k / 2 < m) ? k / 2 : m;
int j = (k / 2 < n) ? k / 2 : n;
if (arr1[i - 1] > arr2[j - 1]) {
return findKth(arr1, m, arr2 + j, n - j, k - j);
} else {
return findKth(arr1 + i, m - i, arr2, n, k - i);
}
}
int main() {
Tanusha Choudhary
0801IT221131
int arr1[] = {2, 3, 6, 7, 9};
int arr2[] = {1, 4, 8, 10};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
int k;
printf("Enter the value of k: ");
scanf("%d", &k);
if (k > 0 && k <= (m + n)) {
printf("The %d-th element in the merged sorted array is %d\n", k, findKth(arr1, m, arr2, n,
k));
} else {
printf("Invalid input for k\n");
}
return 0;
}

Q.4 We have to color ‘m’ boards of length {L1, L2, .. Lm}. There are ‘k’ painters available
and each takes 1 unit time to paint 1 unit of board. The problem is to find the minimum
time to get this job done under the constraints that any painter will only paint continuous
sections of boards, say board {5, 6, 7} or only board {2} or nothing but not board {3, 7, 8}

#include <stdio.h>
#include <limits.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int isPossible(int boards[], int n, int k, int maxTime) {
int paintersRequired = 1;
int currentLength = 0;
for (int i = 0; i < n; i++) {
if (currentLength + boards[i] > maxTime) {
paintersRequired++;
currentLength = boards[i];
if (paintersRequired > k) {
return 0;
Tanusha Choudhary
0801IT221131
}
} else {
currentLength += boards[i];
}
}
return 1;
}
int findMinTime(int boards[], int n, int k) {
int sum = 0;
int maxBoard = INT_MIN;
for (int i = 0; i < n; i++) {
sum += boards[i];
maxBoard = max(maxBoard, boards[i]);
}
int low = maxBoard;
int high = sum;
int result = high;
while (low <= high) {
int mid = (low + high) / 2;
if (isPossible(boards, n, k, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
int main() {
int boards1[] = {5, 5, 5, 5};
int k1 = 2;
int n1 = sizeof(boards1) / sizeof(boards1[0]);
printf("Minimum time to paint boards: %d\n", findMinTime(boards1, n1, k1));
int boards2[] = {5, 10, 15, 20, 25};
int k2 = 2;
int n2 = sizeof(boards2) / sizeof(boards2[0]);
printf("Minimum time to paint boards: %d\n", findMinTime(boards2, n2, k2));
return 0; }
Tanusha Choudhary
0801IT221131
Design and Analysis of Algorithms
Assignment 4

Case Study: Quick Sort in E-Commerce Product Listing


An e-commerce platform needs to sort a large number of products based on their prices to
display the results from lowest to highest. The platform must handle thousands of products
efficiently to provide a smooth user experience.

Problem Definition
• Input: A list of products, where each product has a price attribute.
• Objective: Sort the list of products by price in ascending order.
• Constraints:
o The sorting algorithm should be efficient for large datasets.
o The solution should work within the platform’s memory constraints.
o The sorting operation should be performed in reasonable time to ensure quick page load times
for users.

Code:

#include<stdio.h>
typedef struct{
char name[50];
int price;
}Product;

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


Product temp=*a;
*a=*b;
*b=temp;
}

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


{ int pivot=arr[high].price;

int i=low-1;//Index of smaller element


for(int j=low; j<high; j++){
if(arr[j].price<=pivot){
i++;
swap (&arr[i], &arr[j]);
}
Tanusha Choudhary
0801IT221131
}
swap( &arr[i+1], &arr[high]);
return i+1;
}

void quickSort (Product arr[], int low, int high){


if(low<high){
int pi=partition(arr,low,high);
quickSort(arr,low,pi-1);//Recursively sort elements before partition
quickSort(arr,pi+1,high);//Recursively sort elements after partition
}

void printProducts(Product arr[], int size){


for(int i=0; i<size; i++){
printf("{name:\"%s\",price:%d}\n",arr[i].name,arr[i].price); }
}

int main(){
Product products[]={
{"Laptop",999},
{"Smartphone",699},
{"Tablet",399},
{"Headphones",149},
{"Smartwatch",199}
};
int n=sizeof(products) / sizeof(products[0]);
printf("Originalist:\n");
printProducts(products,n);
quickSort(products,0,n-1);
printf("\nSortedlist:\n");
printProducts(products,n);
return0;
}

You might also like