-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Expand file tree
/
Copy pathtime_complexity_Examples.cpp
More file actions
66 lines (52 loc) · 1.2 KB
/
time_complexity_Examples.cpp
File metadata and controls
66 lines (52 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
/*
Time Complexity
O(1):
Runs in constant time.
The algorithm runs in a fixed number of steps regardless of input size.
Examples: accessing an array element, simple operations.
O(log n):
Number of steps depends on how many times we divide n by 2.
Example: Binary Search.
O(sqrt(n)):
The loop runs until i * i <= n.
Example: checking divisors or prime numbers.
O(n):
The algorithm iterates over all elements once.
O(n log n):
Used in efficient sorting algorithms like Merge Sort and Quick Sort.
O(n^2):
Occurs in nested loops (comparing all pairs).
O(n^3):
Triple nested loops.
Polynomial Time:
O(n^k), where k is constant.
Exponential Time:
O(2^n): subsets, brute force recursion.
O(n!): permutations.
Note:
1 second ≈ 10^8 operations
O(1) -> ∞
O(log n) -> ∞
O(n) -> 1e7
O(n log n) -> 1e6
O(n^2) -> 2000
O(n^3) -> 200
O(2^n) -> 20
O(n!) -> 10
*/
// Example: O(n^2)
void printPairs(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i] << " " << arr[j] << endl;
}
}
}
int main() {
int arr[] = {1, 2, 3};
int n = 3;
printPairs(arr, n);
return 0;
}