0% found this document useful (0 votes)
8 views

Assignment 1 (1)

Uploaded by

shariqqayyum612
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)
8 views

Assignment 1 (1)

Uploaded by

shariqqayyum612
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/ 2

The University of Lahore

Department of Computer Science & IT


CS-09204 Data Structures and Algorithm
Spring 2025
Assignment # 1

Participant ID # ________________ CLO: 2


PLO:
Total Marks: 25 Obtained Marks:

Instructions:

Analyze the following C++ code snippets. For each code snippet, calculate its time complexity. Explain your
reasoning and, if needed, describe how the time complexity is derived. Write down the time complexity
in Big O notation (e.g., O(1), O(n), O(log n), O(n^2)).

Code Snippet 1
C++ Code Time Complexity Analysis
int main() {
int a = 10;
int b = 20;
int result = a + b; // Single operation
cout << "Result: " << result << endl;
return 0;
O(1)
}

Code Snippet 2
C++ Code Time Complexity Analysis
int main() {
int n = 100;
int sum = 0;
for (int i = 1; i <= n; i++) { // Loop from 1 to n

}
sum += i;

cout << "Sum: " << sum << endl;


O(n)
return 0;}
Code Snippet 3
C++ Code Time Complexity Analysis
int main() {
int n = 5;
for (int i = 0; i < n; i++) { // Outer loop
for (int j = 0; j < n; j++) { // Inner loop

}
}
cout << i * j << endl;
O(n^2))
return 0;
}

Code Snippet 4
C++ Code Time Complexity Analysis
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;

found
if (arr[mid] == target) return mid; // Element

else if (arr[mid] < target) low = mid + 1;


O(log n)
else high = mid - 1;
}
return -1; // Element not found
}

Code Snippet 5
C++ Code Time Complexity Analysis
int main() {
int n = 10;
for (int i = 1; i <= n; i *= 2) { // Logarithmic loop
for (int j = 1; j <= n; j++) { // Linear loop inside

}
cout << i + j << endl; O(n)
}
return 0;}

You might also like