DSA-Analysis of Algorithms
DSA-Analysis of Algorithms
DSA-Analysis of Algorithms
▫ If f(x) is a sum of several terms, the one with the largest growth rate is
kept, and all others omitted.
i 1
1
2
3
4
Example
N
• Calculate
i
i 1
3
1 1
2 2N+2
3 4N
4 1
Log
• The time Complexity of a loop is considered as
O(Log n) if the loop variables are divided/multiplied
by a constant amount.
• And also for recursive calls in the recursive function,
the Time Complexity is considered as O(Log n).
22
Example
for (int i = 1; i <= n; i *= c) {
// some O(1) expressions
} • O(log n)
for (int i = n; i > 0; i /= c) {
// some O(1) expressions
}
Comparison: Discussion
Parameters Bubble Sort Selection Insertion
Sort Sort
Comparison same same Less
comparison
Swaps swaps swaps n times
almost n*(n-1) at most
Best case O(n) O(n2) O(n)
Complexity
Worst Case O(n2) O(n2) O(n2)
Comparison: Discussion
• Reading Assignment
2
3
5
6
7
8
9
• O(N3)