You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Big-O Notation.markdown
+60-44Lines changed: 60 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -24,103 +24,119 @@ Some examples to better understand the Big(O) notation:
24
24
25
25
The most common example with O(1) complexity is accessing an array index.
26
26
27
-
```c++
28
-
int i = a[5];
27
+
```swift
28
+
let value=array[5]
29
29
```
30
30
31
31
Another example of O(1) is Pushing and Popping from Stack.
32
32
33
33
34
34
**O(log n)**
35
35
36
-
```c++
37
-
for(int i=0; i<n; i *= 2){
38
-
cout<<i<<endl; // instead of simply incrementing, 'i' is increased by 2 times itself in each run.
36
+
```swift
37
+
var j =1
38
+
while j < n {
39
+
// do constant time stuff
40
+
j *=2
39
41
}
40
-
```
42
+
```
43
+
44
+
Instead of simply incrementing, 'j' is increased by 2 times itself in each run.
45
+
41
46
Binary Search Algorithm is an example of O(log n) complexity.
42
47
43
48
44
49
**O(n)**
45
50
46
-
```c++
47
-
for(int i=0; i<n; i++){
48
-
cout<<a[i]<<endl;
49
-
}
51
+
```swift
52
+
for i instride(from: 0, to: n, by: 1) {
53
+
print(array[i])
54
+
}
50
55
```
51
56
52
57
Array Traversal and Linear Search are examples of O(n) complexity.
53
58
54
59
55
60
**O(n log n)**
56
61
57
-
```c++
58
-
for(int i = 0; i < n; i++) { //linear
59
-
for(int j = 1; j < n; j *= 2){ // log (n)
60
-
//do constant time stuff
62
+
```swift
63
+
for i instride(from: 0, to: n, by: 1) {
64
+
var j =1
65
+
while j < n {
66
+
j *=2
67
+
// do constant time stuff
68
+
}
69
+
}
70
+
```
71
+
72
+
OR
73
+
74
+
```swift
75
+
for i instride(from: 0, to: n, by: 1) {
76
+
funcindex(afteri: Int) ->Int? { // multiplies `i` by 2 until `i` >= `n`
77
+
return i < n ? i *2:nil
78
+
}
79
+
for j insequence(first: 1, next: index(after:)) {
80
+
// do constant time stuff
61
81
}
62
82
}
63
-
```
83
+
```
64
84
65
85
Merge Sort and Heap Sort are examples of O(n log n) complexity.
66
86
67
87
68
88
**O(n^2)**
69
89
70
-
```c++
71
-
for(int i = 0; i < n; i++) {
72
-
for(int j = 1; j < n; j++){
73
-
//do constant time stuff
90
+
```swift
91
+
for i instride(from: 0, to: n, by: 1) {
92
+
for j instride(from: 1, to: n, by: 1) {
93
+
//do constant time stuff
74
94
}
75
-
}
95
+
}
76
96
```
77
97
78
98
Traversing a simple 2-D array and Bubble Sort are examples of O(n^2) complexity.
79
99
80
100
81
101
**O(n^3)**
82
102
83
-
```c++
84
-
for(int i = 0; i < n; i++) {
85
-
for(int j = 1; j < n; j++){
86
-
for(int k = 1; k < n; k++){
87
-
//do constant time stuff
103
+
```swift
104
+
for i instride(from: 0, to: n, by: 1) {
105
+
for j instride(from: 1, to: n, by: 1) {
106
+
for k instride(from: 1, to: n, by: 1) {
107
+
//do constant time stuff
88
108
}
89
109
}
90
-
}
110
+
}
91
111
```
92
112
93
113
**O(2^n)**
94
114
95
115
Algorithms with running time O(2^N) are often recursive algorithms that solve a problem of size N by recursively solving two smaller problems of size N-1.
96
116
The following example prints all the moves necessary to solve the famous "Towers of Hanoi" problem for N disks.
97
117
98
-
```c++
99
-
void solve_hanoi(int N, string from, string to, string spare) {
100
-
if (N<1) {
101
-
return;
102
-
}
103
-
if (N>1) {
104
-
solve_hanoi(N-1, from, spare, to);
118
+
```swift
119
+
funcsolveHanoi(N: Int, from: String, to: String, spare: String) {
120
+
guard n >=1else { return }
121
+
if N >1 {
122
+
solveHanoi(N: N -1, from: from, to: spare, spare: to)
123
+
} else {
124
+
solveHanoi(N: N-1, from: spare, to: to, spare: from)
105
125
}
106
-
print "move from " + from + " to " + to;
107
-
if (N>1) {
108
-
solve_hanoi(N-1, spare, to, from);
109
-
}
110
-
}
126
+
}
111
127
```
112
128
113
129
114
130
**O(n!)**
115
131
116
132
The most trivial example of function that takes O(n!) time is given below.
117
133
118
-
```c++
119
-
voidnFacFunc(int n) {
120
-
for(int i=0; i<n; i++) {
121
-
nFacFunc(n-1);
122
-
}
123
-
}
134
+
```swift
135
+
funcnFacFunc(n: Int) {
136
+
for i instride(from: 0, to: n, by: 1) {
137
+
nFactFunc(n -1)
138
+
}
139
+
}
124
140
```
125
141
126
142
Often you don't need math to figure out what the Big-O of an algorithm is but you can simply use your intuition. If your code uses a single loop that looks at all **n** elements of your input, the algorithm is **O(n)**. If the code has two nested loops, it is **O(n^2)**. Three nested loops gives **O(n^3)**, and so on.
0 commit comments