0% found this document useful (0 votes)
5 views3 pages

cs502 1

This document contains an assignment for a CS502 course on algorithms, focusing on two questions. The first question involves a C++ program that calculates and prints even numbers and Fibonacci numbers based on user input, along with an analysis of its time complexity, which is determined to be O(n*m). The second question requires students to illustrate the divide and combine phases of the Merge Sort algorithm using a provided list of digits.

Uploaded by

Mani Befikra
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)
5 views3 pages

cs502 1

This document contains an assignment for a CS502 course on algorithms, focusing on two questions. The first question involves a C++ program that calculates and prints even numbers and Fibonacci numbers based on user input, along with an analysis of its time complexity, which is determined to be O(n*m). The second question requires students to illustrate the divide and combine phases of the Merge Sort algorithm using a provided list of digits.

Uploaded by

Mani Befikra
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/ 3

Bc22040512

CS502- Fundamental of Algorithms Spring 2025


Assignment no 1
Question No 1:-
Program Code:
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter number of rows: "; // O(1) - executes once
cin >> n; // O(1) - executes once
cout << "Enter maximum value: "; // O(1) - executes once
cin >> m; // O(1) - executes once
int even = 2; // O(1) - executes once
// Starting even number
int f1 = 0, f2 = 1, fib = 0; // O(1) - executes once
// Fibonacci series first numbers
for (int i = 1; i <= n; i++) // O(n) - executes n+1 times
{ // Outer loop over rows:
cout << "Row " << i << ": "; (inside loop) // O(n) - executes n times
if (i % 2 != 0) { // O(n) - executes n+1 times
// If row is odd
for (int j = 0; even <= m && j < m; j++) { // Executes at most O(m) times per odd now
cout << even << " "; // Executes at most O(m/2) times per odd now
even += 2; // Executes at most O(m/2) times per odd now
}
}
else { // If row is even
while (fib <= m) { // Executes O(log m) times per even row
cout << fib << " "; // Executes O(log m) times per even row
f1 = f2; // Executes O(log m) times per even row
f2 = fib; // Executes O(log m) times per even row
fib = f1 + f2; // Executes O(log m) times per even row
}
}
cout << endl; // O(n) - executes n times
}
return 0; // O(1) – executes once
}
Analyses of Complexity in Time:
1. Statements for initialization: Each is executed once - O(1)
2. Outer loop: Executes n times - O(n)
3. Odd rows (approximately n/2 rows):
o Inner loop executes at most m/2 times (since we're printing even numbers up to m)
о Total for odd rows: O(n/2 m/2) = O(n*m/4)
4. Even rows (approximately n/2 rows):
0 The Fibonacci sequence grows exponentially
。 Approximately log_p(m), where q is the golden ratio, is the number of Fibonacci numbers
smaller than m. o Total for even rows: O(n/2 log m) = O(n log m/2)
Total Time Complexity:
0(1) + O(n) + O(n×m/4) + O(n log m/2)
Since we take the highest-order term, the overall time complexity is: O(nxm) - This is because
the term with m dominates the log m term for large values of m.

Question No 2:-
Consider the following digits as a list [78, 45, 89, 32, 90, 12, 67, 55, 99, 21]
Note: You are required to draw a separate tree structure for both the Divide and
Combine phase of Merge Sort mechanism.

Divide Phase:-
Combine Phase:-

You might also like