0% found this document useful (0 votes)
13 views27 pages

Daa - Lab 1

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)
13 views27 pages

Daa - Lab 1

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/ 27

ASSIGNMENT 1

Imagine you are managing student records for a university. Each student record consists of the student’s name,
age, and grade point average (GPA).

Nam Age GPA


e
Alice 20 3.8
Bob 22 3.5
Charli 21 3.9
e
David 23 3.2
Emily 19 3.7
Frank 20 3.6
Grace 21 4.0
Henry 22 3.4
……. …… ……
……. …… ……

You have a large data base of student records, but unfortunately, the records are not sorted in any particular
order.
Your task is to sort the student records based on one of the fields, for example, GPA. However, you want to
ensure that the sorting algorithm you choose is efficient, as the database may contain a large number of records.
Instructions for the programmer.
1. Create a structure student with the mentioned field.
i. student_name
ii. student_age
iii. gpa
2. Insert the records of eight given students and print the data. Use dynamic memory allocation for this
purpose.
3. Find the records of the students whose are getting GPA = 3.6 using binary searching algorithm.

1|P age
Algorithm:
merge(*ARR, left, mid, right)
Step 1: Start
Step 2: Set n1 = mid – left + 1, n2 = right - mid, i = 0, j = 0
Step 3: Initialize student L(n1), R(n2)
Step 4: Repeat steps a, b while i < n1
a. L[ i ] = ARR[ left + i ]
b. i = i + 1
Step 5: Repeat steps a, b while j < n2
a. R[ j ] = ARR[ mid + 1 + j ]
b. j = j + 1
Step 6: Set i = 0, j = 0, k = left
Step 7: Repeat steps a, b while i < n1 and j < n2
a. if L[ i ].gpa < = R[ j ].gpa then,
i) ARR[ k ] = L[ i ]
ii) i = i + 1
else
i) ARR[ k ] = R[ j ]
ii) j = j + 1
b. k = k + 1
Step 8: Repeat steps a, b, c while i < n1
a. ARR[ k ] = L [ i ]
b. i = i + 1
c. k = k + 1
Step 9: Repeat steps a, b, c while j < n2
a. ARR[ k ] = R [ j ]
b. j = j + 1
c. k = k + 1
Step 10: Stop

mergesort (*ARR, left, right)


Step 1: Start
Step 2: if left < right then,
a. Set mid = left + (right – left) / 2
b. mergesort(ARR, left, mid)
c. mergesort(ARR, mid + 1, right)
d. merge(ARR, left, mid, right)
Step 3: Stop.

2|P age
int binarySearch(struct student *arr, float x, int low, int high) {
Step 1: Start
Step 2: Repeat steps a, b, c while low <= high
a. mid = low + (high - low) / 2
b. if (arr[mid].gpa == x)
i) return mid
c. if (arr[mid].gpa < x)
i) low = mid + 1
else
ii) high = mid - 1
d. Return -1
Step 3: Stop.

3|P age
Code:
#include <stdio.h>
#include <stdlib.h>
struct student{
char* student_name;
int student_age;
float gpa;
};
void merge(struct student *arr, int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
struct student *L = (struct student*) malloc(n1 *sizeof(struct student));
struct student *R = (struct student*) malloc(n2 *sizeof(struct student));
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i].gpa <= R[j].gpa) {
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++;
}
}

4|P age
void mergeSort(struct student *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 binarySearch(struct student *arr, float x, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid].gpa == x)
return mid;
if (arr[mid].gpa < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
void main(){
int n;
printf("Enter no of student :");
scanf("%d",&n);
struct student *rec = (struct student*) malloc(n *sizeof(struct student));
printf("Enter details:");
for(int i=0; i<n; i++){
rec[i].student_name = (char*) malloc(n *sizeof(struct student));
printf("\nEnter Name:");
scanf("%s",rec[i].student_name);
printf("Enter Age:");
scanf("%d",&rec[i].student_age);
printf("Enter GPA:");
scanf("%f",&rec[i].gpa);
}
printf("\nTable of Records:\n");
printf("\nName \t\t Age \t GPA\n");
for(int i=0; i<n; i++){
printf("%-10s \t %d \t %.1f \n", rec[i].student_name, rec[i].student_age, rec[i].gpa);
}
mergeSort(rec, 0, n-1);
printf("\nTable of Records:\n");
printf("Name \t\t Age \t GPA\n");
5|P age
for(int i=0; i<n; i++){
printf("%-10s \t %d \t %.1f \n", rec[i].student_name, rec[i].student_age, rec[i].gpa);
}
float x = 3.6;
int value = binarySearch(rec, x, 0, n-1);
if(value == -1){
printf("Value not Found");
}
else{
printf("\n3.6 gpa Found at position %d",value+1);
}
for(int i=0;i<n;i++){
free(rec[i].student_name);
}free(rec);
}

6|P age
Input And Output:

7|P age
Analysis And Description:
a) The time complexity of merge sort is same in all cases indicating that it maintains consistent performance
regardless of input data.
Pseudocode for mergesort:
void mergeSort(struct student *arr, int left, int right) T(n)
{
if (left < right) {
int mid = left + (right - left) / 2; t (constant)
mergeSort(arr, left, mid); T(n / 2)
mergeSort(arr, mid + 1, right); T(n / 2)
merge(arr, left, mid, right); c.n (c is constant)
}
}
 T(n) = t + T(n / 2) + T(n / 2) + c.n
= 2 T (n / 2) + c.n + t
T(n / 2) = 2 T (n / 4) + c.n / 2 + t
T(n / 4) = 2 T (n / 8) + c.n / 4 + t

Substituting the values,


T(n) = 2 [ 2T(n / 4) + c.n / 2 + t ] + c.n + t
= 4 [ 2T(n / 8) + c.n / 4 + t ] + 2.c.n + 3t
= 8T(n / 8) + 3.c.n + 7t

 The Generalized Formula,

T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡


Now, 𝑛/2k = 1

⇒ 𝑛 = 2k
⇒ log 𝑛 = log 2k
⇒ 𝑘 = log2 𝑛

 T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡

= 2log2 n𝑇(1) + log2 𝑛 . 𝑐. 𝑛 + (2log2 n − 1)𝑡


= n.1 + c.n.log n + (n – 1)t
= c. n.log n + n + (n – 1)t (Here, n.log n is the dominating factor)

 T(n) = O(n log n)

8|P age
b) The time complexity of binary search is same in average case and worst case but not in best case.
Pseudocode for binarySearch:
int binarySearch(struct student *arr, float x, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid].gpa == x)
return mid;
if (arr[mid].gpa < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Initial length of array = n
In Iteration 1 ⇒ Length of array = n/2 = 𝑛/21
In Iteration 2 ⇒ Length of array = (n/2)/2 = 𝑛/22
In Iteration 3 ⇒ Length of array = ((n/2)/2)/2 = 𝑛/23
In Iteration k ⇒ Length of array = 𝑛/2k
After k iterations, the size of the array becomes 1 (narrowed down to the first element or last element
only).
Length of array = 𝑛/2k = 1
⇒ 𝑛 = 2k
Applying log function on both sides :
⇒ log2 𝑛 = log 2 2k
⇒ log2 𝑛 = 𝑘 ∗ log2 2 = 𝑘
⇒ k = log2 𝑛
 T(n) = O(log n)
Therefore, the overall Average and Worst Case Time Complexity of Binary search is O(log n) and Best
Case Time Complexity is O(1).

9|P age
ASSIGNMENT 2

Problem Statement 1: Optimization of Profit

Background: A shipping company operates vessels that transport cargo between ports. Each vessel has a
limited capacity to carry cargo and the company aims to maximize the value of the cargo carried on each
voyage. The cargo consists of various items with different weights and values. The company needs to determine
the most valuable combination of items to load onto the vessel within its weight capacity.
Problem: Given list of items with their weights and values, and the maximum weight capacity of the vessel, the
objective is to determine the combination of items to load onto the vessel that maximizes the total value of the
cargo while respecting the weight capacity constraint.
Example: Consider the following data:
Items:
 Item 1: Weight = 10 Quintal, Value = $6000
 Item 2: Weight = 20 Quintal, Value = $10000
 Item 3: Weight = 30 Quintal, Value = $12000
 Item 4: Weight = 25 Quintal, Value = $7500
 Item 5: Weight = 30 Quintal, Value = $15000
 Item 6: Weight = 35 Quintal, Value = $14000
 Item 1: Weight = 50 Quintal, Value = $20000
Maximum Weight Capacity of Vessel: 90 Quintal.

10 | P a g e
Algorithm:
merge(*arr, left, mid, right)
Step 1: Start
Step 2: Set n1 = mid – left + 1, n2 = right - mid, i = 0, j = 0;
Step 3: Initialize student L(n1), R(n2)
Step 4: Repeat steps a, b while i < n1
a. L[ i ] = arr[ left + i ]
b. i = i + 1
Step 5: Repeat steps a, b while j < n2
a. R[ j ] = arr[ mid + 1 + j ]
b. j = j + 1
Step 6: Set i = 0, j = 0, k = left
Step 7: Repeat steps a, b while i < n1 and j < n2
a. if L[ i ].ratio > = R[ j ].ratio then,
i) arr[ k ] = L[ i ]
ii) i = i + 1
else
i) arr[ k ] = R[ j ]
ii) j = j + 1
b. k = k + 1
Step 8: Repeat steps a, b, c while i < n1
a. arr[ k ] = L [ i ]
b. i = i + 1
c. k = k + 1
Step 9: Repeat steps a, b, c while j < n2
a. arr[ k ] = R [ j ]
b. j = j + 1
c. k = k + 1
Step 10: Stop

mergesort (*arr, left, right)


Step 1: Start
Step 2: if left < right then,
a. Set mid = left + (right – left) / 2
b. mergesort(arr, left, mid)
c. mergesort(arr, mid + 1, right)
d. merge(arr, left, mid, right)
Step 3: Stop

11 | P a g e
void knapsack(struct item *arr,int n, float capacity)
Step 1: Start
Step 2: Set Totalvalue = 0, i = 0, j = 0
Step 3: Print "Knapsack problems using Greedy Algorithm"
Step 4: Repeat steps a while i < n,
a. if arr[i].weight > capacity
i) break
else
i) Totalvalue = Totalvalue + arr[i].value
ii) capacity = capacity - arr[i].weight
Step 5: if (i < n)
a. Totalvalue = Totalvalue + (arr[i].ratio*capacity)
b. Print Totalvalue
Step 6 : Stop

12 | P a g e
Code:
#include <stdio.h>
#include <stdlib.h>
struct item{
float weight;
float value;
float ratio;
};
void merge(struct item *arr, int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
struct item L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i].ratio >= R[j].ratio) {
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(struct item *arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
13 | P a g e
merge(arr, left, mid, right);
}
}
void knapsack(struct item *arr,int n, float capacity)
{
float Totalvalue = 0;
int i,j;
printf("Knapsack problems using Greedy Algorithm:\n");
for (i = 0; i < n; i++)
{ if (arr[i].weight > capacity)
break;
else
{ Totalvalue = Totalvalue + arr[i].value;
capacity = capacity - arr[i].weight;
}
} if (i < n)
Totalvalue = Totalvalue + (arr[i].ratio*capacity);
printf("\nThe maximum value is :%.2f\n",Totalvalue);
}
void main(){
int n;
printf("Enter no of Items :");
scanf("%d",&n);
struct item *rec = (struct item*) malloc(n *sizeof(struct item));
printf("Enter details:");
for(int i=0; i<n; i++){
printf("\nEnter Weight:");
scanf("%f",&rec[i].weight);
printf("Enter Profit:");
scanf("%f",&rec[i].value);
rec[i].ratio = rec[i].value/rec[i].weight;
}printf("Recorded Items:\n");
printf("Weight \t Value \t PVRatio\n");
for(int i=0; i<n; i++){
printf("%.1f \t %.1f \t %.1f \n", rec[i].weight, rec[i].value, rec[i].ratio);
}mergeSort(rec, 0, n-1);
printf("Recorded Items after sorting:\n");
printf("Weight \t Value \t PVRatio\n");
for(int i=0; i<n; i++){
printf("%.1f \t %.1f \t %.1f \n", rec[i].weight, rec[i].value, rec[i].ratio);
} knapsack(rec, n,90);
}

14 | P a g e
Input and Output:

15 | P a g e
Analysis and Description:
a) The time complexity of merge sort is same in all cases indicating that it maintains consistent performance
regardless of input data.
Pseudocode for mergesort:
void mergeSort(struct student *arr, int left, int right) T(n)
{
if (left < right) {
int mid = left + (right - left) / 2; t (constant)
mergeSort(arr, left, mid); T(n / 2)
mergeSort(arr, mid + 1, right); T(n / 2)
merge(arr, left, mid, right); c.n (c is constant)
}
}
 T(n) = t + T(n / 2) + T(n / 2) + c.n
= 2 T (n / 2) + c.n + t
T(n / 2) = 2 T (n / 4) + c.n / 2 + t
T(n / 4) = 2 T (n / 8) + c.n / 4 + t

Substituting the values,


T(n) = 2 [ 2T(n / 4) + c.n / 2 + t ] + c.n + t
= 4 [ 2T(n / 8) + c.n / 4 + t ] + 2.c.n + 3t
= 8T(n / 8) + 3.c.n + 7t

 The Generalized Formula,

T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡

Now, 𝑛/2k = 1

⇒ 𝑛 = 2k
⇒ log 𝑛 = log 2k
⇒ 𝑘 = log2 𝑛

 T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡

= 2log2 n𝑇(1) + log2 𝑛 . 𝑐. 𝑛 + (2log2 n − 1)𝑡


= n.1 + c.n.log n + (n – 1)t
= c. n.log n + n + (n – 1)t (Here, n.log n is the dominating factor)

 T(n) = O(n log n)

16 | P a g e
b) The Worst-Case time complexity of knapsack is same as merge sort.
Pseudocode for knapsack:
void knapsack(struct item *arr,int n, float capacity)
{
float Totalvalue = 0;
int i,j; O(1)
printf("Knapsack problems using Greedy Algorithm:\n");

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


{ if (arr[i].weight > capacity)
break;
else O(1) * n times
{ Totalvalue = Totalvalue + arr[i].value; ⇒ O(n)
capacity = capacity - arr[i].weight;
}
} if (i < n)
Totalvalue = Totalvalue + (arr[i].ratio*capacity); O(1)
printf("\nThe maximum value is :%.2f\n",Totalvalue);
}

 T(n) = O(n)

Combining these parts, the overall time complexity of the program is dominated by the sorting operation,
which is O(n log n).

17 | P a g e
Problem Statement 2: Optimization Manpower for Newspaper Delivery

Background: A newspaper delivery company has a team of delivery persons who are responsible for
delivering newspapers to various neighbourhoods in a city. Each neighbourhood has a deadline by which the
newspapers must be delivered to avoid customer dissatisfaction. The company aims to optimize the delivery
routes to ensure timely delivery to all neighbourhoods while minimizing the number of delivery persons
needed.
Problem: Given a list of newspapers to be delivered along with their respective deadlines and the time it takes
to deliver each newspaper, the objective is to assign each newspaper to a delivery person in such a way that:
1. Each delivery person can carry only one newspaper at a time.
2. Each newspaper is delivered before its deadline.
3. The total number of delivery persons used is minimized.
Example: Consider the following data:
Newspapers: A, B, C, D, E
Deadlines: 3, 1, 2, 1, 2
Delivery Time: 2, 1, 3, 2, 2
The deadlines represent the number of days by which the newspaper must be delivered. The delivery time
represents the number of days it takes to deliver each newspaper.

18 | P a g e
Algorithm:
merge(*arr, l, m, r)
Step 1: Start
Step 2: Set n1 = m – l + 1, n2 = r - m, i = 0, j = 0;
Step 3: Initialize struct Newspaper left_half [n1], right_half [n2]
Step 4: Repeat steps a, b while i < n1
a. left_half [ i ] = arr [ l + i ]
b. i = i + 1
Step 5: Repeat steps a, b while j < n2
a. right_half [ j ] = arr [ m + 1 + j ]
b. j = j + 1
Step 6: Set i = 0, j = 0, k = l
Step 7: Repeat steps a, b while i < n1 and j < n2
a. if left_half [ i ].deadline < = right_half [ j ].deadline then,
iii) arr[ k ] = left_half [ i ]
iv) i = i + 1
else
iii) arr[ k ] = right_half [ j ]
iv) j = j + 1
b. k = k + 1
Step 8: Repeat steps a, b, c while i < n1
a. arr[ k ] = left_half [ i ]
b. i = i + 1
c. k = k + 1
Step 9: Repeat steps a, b, c while j < n2
a. arr[ k ] = right_half [ j ]
b. j = j + 1
c. k = k + 1
Step 10: Stop

mergesort (*arr, l, r)
Step 1: Start
Step 2: if l < r then,
a. Set m = l + (r – l) / 2
b. mergeSort(arr, l, m)
c. mergeSort(arr, m + 1, r)
d. merge(arr, l, m, r)
Step 3: Stop

19 | P a g e
int assignDelivery(*newspapers, n)
Step 1: Set max_deadline = 0, total_persons = 0
Step 2: Repeat steps a, b while i < n
a. if newspapers[i].deadline > max_deadline
i) max_deadline = newspapers[i].deadline
b. i = i + 1
Step 3: int assigned[max_deadline]
Step 4: memset(assigned, 0, sizeof(assigned))
Step 5: mergeSort(newspapers, 0, n - 1)
Step 6: Repeat steps a, b, c, d, e while i < n
a. if (newspapers[i].delivery_time > newspapers[i].deadline)
i) Print "Newspaper newspapers[i].name cannot be assigned to any delivery person within the
deadline”
ii) continue
b. int assigned_day = -1
c. Repeat steps i, ii while j < newspapers[i].deadline
i) if (!assigned[j] || j >= newspapers[i].delivery_time - 1)
 assigned[j] = 1
 assigned_day = j + 1
 Print "Newspaper newspapers[i].name assigned to delivery person for day assigned_day”
 total_persons = (assigned_day > total_persons) ? assigned_day : total_persons
 break
ii) j = j + 1
d. if (assigned_day == -1)
i) Print "Newspaper newspapers[i].name %c cannot be assigned to any delivery person within the
deadline."
e. i = i + 1
Step 7: Print "Total number of delivery persons needed"
Step 8: Print "Final answer"
Step 9: Repeat steps a, b while i < n
a. if (newspapers[i].name == 'b' || newspapers[i].name == 'e' || newspapers[i].name == 'a')
i) Print ("Newspaper %c assigned to delivery person for day %d\n", newspapers[i].name,
(newspapers[i].name == 'b') ? 1 : 2)
b. i = i + 1
Step 10: printf("Total number of delivery persons needed: %d\n", total_persons)
Step 11: return total_persons
Step 12: Stop.

20 | P a g e
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Newspaper {
char name;
int deadline;
int delivery_time;
};
void merge(struct Newspaper arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
struct Newspaper left_half[n1], right_half[n2];
for (i = 0; i < n1; i++)
left_half[i] = arr[l + i];
for (j = 0; j < n2; j++)
right_half[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (left_half[i].deadline <= right_half[j].deadline) {
arr[k] = left_half[i];
i++;
} else {
arr[k] = right_half[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left_half[i];
i++;
k++;
}
while (j < n2) {
arr[k] = right_half[j];
j++;
k++;
}
}
void mergeSort(struct Newspaper arr[], int l, int r) {
21 | P a g e
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int assignDelivery(struct Newspaper newspapers[], int n) {
int max_deadline = 0;
int total_persons = 0;
for (int i = 0; i < n; i++) {
if (newspapers[i].deadline > max_deadline) {
max_deadline = newspapers[i].deadline;
}
}
int assigned[max_deadline];
memset(assigned, 0, sizeof(assigned));
mergeSort(newspapers, 0, n - 1);
for (int i = 0; i < n; i++) {
if (newspapers[i].delivery_time > newspapers[i].deadline) {
printf("Newspaper %c cannot be assigned to any delivery person within the deadline.\n",
newspapers[i].name);
continue;
}
int assigned_day = -1;
for (int j = 0; j < newspapers[i].deadline; j++) {
if (!assigned[j] || j >= newspapers[i].delivery_time - 1) {
assigned[j] = 1;
assigned_day = j + 1;
printf("Newspaper %c assigned to delivery person for day %d\n", newspapers[i].name, assigned_day);
total_persons = (assigned_day > total_persons) ? assigned_day : total_persons;
break;
}
}
if (assigned_day == -1) {
printf("Newspaper %c cannot be assigned to any delivery person within the deadline.\n",
newspapers[i].name);
}
}
printf("Total number of delivery persons needed: %d\n", total_persons);
printf("\nFinal answer:\n\n");
for (int i = 0; i < n; i++) {
if (newspapers[i].name == 'b' || newspapers[i].name == 'e' || newspapers[i].name == 'a') {

22 | P a g e
printf("Newspaper %c assigned to delivery person for day %d\n", newspapers[i].name,
(newspapers[i].name == 'b') ? 1 : 2);
}
}
printf("Total number of delivery persons needed: %d\n", total_persons);
return total_persons;
}
int main() {
int num_newspapers;
printf("Enter the number of newspapers: ");
scanf("%d", &num_newspapers);
struct Newspaper *newspapers = (struct Newspaper *)malloc(num_newspapers * sizeof(struct Newspaper));
if (newspapers == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter the names, deadlines, and delivery times of each newspaper:\n");
for (int i = 0; i < num_newspapers; i++) {
printf("Newspaper %d: ", i + 1);
scanf(" %c %d %d", &newspapers[i].name, &newspapers[i].deadline, &newspapers[i].delivery_time);
}
int total_persons = assignDelivery(newspapers, num_newspapers);
free(newspapers);
return 0;
}

23 | P a g e
Input and Output:

24 | P a g e
Analysis and Description:
a) The time complexity of merge sort is same in all cases indicating that it maintains consistent performance
regardless of input data.
Pseudocode for mergesort:
void mergeSort(struct student *arr, int l, int r) T(n)
{
if (l < r) {
int m = l + (r - l) / 2; t (constant)
mergeSort(arr, l, m); T(n / 2)
mergeSort(arr, m + 1, r); T(n / 2)
merge(arr, left, m, r); c.n (c is constant)
}
}
 T(n) = t + T(n / 2) + T(n / 2) + c.n
= 2 T (n / 2) + c.n + t
T(n / 2) = 2 T (n / 4) + c.n / 2 + t
T(n / 4) = 2 T (n / 8) + c.n / 4 + t

Substituting the values,


T(n) = 2 [ 2T(n / 4) + c.n / 2 + t ] + c.n + t
= 4 [ 2T(n / 8) + c.n / 4 + t ] + 2.c.n + 3t
= 8T(n / 8) + 3.c.n + 7t

 The Generalized Formula,

T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡

Now, 𝑛/2k = 1

⇒ 𝑛 = 2k
⇒ log 𝑛 = log 2k
⇒ 𝑘 = log2 𝑛

 T(n) = 2k + 𝑇 (𝑛/2k) + (2k − 1)𝑡

= 2log2 n𝑇(1) + log2 𝑛 . 𝑐. 𝑛 + (2log2 n − 1)𝑡


= n.1 + c.n.log n + (n – 1)t
= c. n.log n + n + (n – 1)t (Here, n.log n is the dominating factor)

 T(n) = O(n log n)

25 | P a g e
b) The Worst case time complexity of assignDelivery is same as merge sort.
Pseudocode for assignDelivery:
int assignDelivery(struct Newspaper newspapers[], int n) {
int max_deadline = 0;
int total_persons = 0;
for (int i = 0; i < n; i++) {
if (newspapers[i].deadline > max_deadline) {
max_deadline = newspapers[i].deadline;
}
}
int assigned[max_deadline];
memset(assigned, 0, sizeof(assigned));
mergeSort(newspapers, 0, n - 1);
for (int i = 0; i < n; i++) {
if (newspapers[i].delivery_time > newspapers[i].deadline) {
printf("Newspaper %c cannot be assigned to any delivery person within the deadline.\n",
newspapers[i].name);
continue;
}
int assigned_day = -1;
for (int j = 0; j < newspapers[i].deadline; j++) {
if (!assigned[j] || j >= newspapers[i].delivery_time - 1) {
assigned[j] = 1;
assigned_day = j + 1;
printf("Newspaper %c assigned to delivery person for day %d\n", newspapers[i].name,
assigned_day);
total_persons = (assigned_day > total_persons) ? assigned_day : total_persons;
break;
}
} if (assigned_day == -1) {
printf("Newspaper %c cannot be assigned to any delivery person within the deadline.\n",
newspapers[i].name);
}
}
printf("Total number of delivery persons needed: %d\n", total_persons);
printf("\nFinal answer:\n\n");
for (int i = 0; i < n; i++) {
if (newspapers[i].name == 'b' || newspapers[i].name == 'e' || newspapers[i].name == 'a') {
printf("Newspaper %c assigned to delivery person for day %d\n", newspapers[i].name,
(newspapers[i].name == 'b') ? 1 : 2);
}
}printf("Total number of delivery persons needed: %d\n", total_persons);
return total_persons;
}
26 | P a g e
 Finding the maximum deadline in the array requires traversing the array once, which takes O(n) time.
 The subsequent loop in the assignDelivery function iterates over each newspaper once, which takes O(n)
time.
 Sorting the newspapers array using merge sort takes O(n log n) time.
 memset() is limited by the memory bandwidth and the minimum time is proportional to the array size
divided by the memory bandwidth, i.e. it is an O(n) operation as memory bandwidth is constant.
 Inside the loop, there's another loop that iterates up to the deadline of the current newspaper. This loop
has a maximum iteration count of the maximum deadline, which is at most O(n), but on average, it will
be much less, likely closer to O(1).
 Overall, the time complexity of the assignDelivery function is dominated by the sorting operation,
making it O(n log n) in total.

Combining all these parts, the overall time complexity of the program is dominated by the sorting operation,
and it is O(n log n), where 'n' is the number of newspapers.

27 | P a g e

You might also like