CS 2321 (Data Structures and Algorithms)
Assignment 1 (Algorithm Analysis)
1. 𝑷𝒓𝒐𝒗𝒆
𝟓𝒏 𝒍𝒐𝒈 𝟐 𝒏 + 𝟖𝒏 − 𝟐𝟎𝟎 = 𝑶(𝒏 𝒍𝒐𝒈 𝟐 𝒏)
𝒏𝟐 + 𝟒𝟐𝒏 + 𝟕 = 𝑶(𝒏𝟐 )
2. Find Big O notation
𝟐 𝒍𝒐𝒈 𝟐 𝒏 + 𝟐 = 𝑶…………….
𝒏 + 𝟐 = 𝑶…………….
𝟐𝒏 + 𝟏𝟓𝒏𝟏/𝟐 = 𝑶…………….
𝟓 𝒍𝒐𝒈 𝟐 𝒏 + 𝟐𝒏𝟑 = 𝑶…………….
𝒏 𝒍𝒐𝒈 𝟐 𝒏 + 𝒏𝟐 = 𝑶…………….
3. What is the worst-case complexity of the each of the following code
fragments?
a) Two loops in a row:
for (i = 0; i < N; i++) {
sequence of statements
}
for (j = 0; j < M; j++) {
sequence of statements
}
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
How would the complexity change if the second loop went to N instead of M?
b) A nested loop followed by a non-nested loop:
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
sequence of statements
}
}
for (k = 0; k < N; k++) {
sequence of statements
}
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
c) A nested loop in which the number of times the inner loop executes
depends on the value of the outer loop index:
for (i = 0; i < N; i++) {
for (j = N; j > i; j--) {
sequence of statements
}
}
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
4. Consider the following (simple) code:
for (int i = 0 ; i < N; i++) {
a[i] = i ;
}
Calculate the number of operations Find Big O, Ω and 𝜽 notation
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
Find Big O, Ω and 𝜽 notation
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
……………………………………………………………………………………………………
Do your best
Wamda