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: contents/notation/notation.md
+24-6
Original file line number
Diff line number
Diff line change
@@ -21,10 +21,28 @@ In addition, there are many different notations depending on who you ask, but fo
21
21
Big $$O$$ assumes the worst, which is the often the most useful description of an algorithm.
22
22
On the other hand, $$\Omega$$ assumes the best and $$\Theta$$ is used when the best and worst cases are the same.
23
23
24
+
It *may* seems like strange that an algorithm can run in different time, but let me explain a while:
25
+
```julia
26
+
functionconstant(a::UInt64, b::UInt64)
27
+
println(b)
28
+
for i=0:18446744073709551615
29
+
if(a < b)
30
+
b = a - b
31
+
println(b)
32
+
end
33
+
end
34
+
end
35
+
```
36
+
If we calculates the big 3 in b, it will be $$\Omega(1)$$ and $$O(b)$$, Obviously not the same.
37
+
The best-case runtime will be $$1$$`println` statement if a > b, the worst-case runtime will be $$b$$`println` statement if a = 1.
38
+
So that's the explanation, and let's move on.
39
+
24
40
Of the three Big $$O$$ is used the most, and is used in conversation to mean that the algorithm will take "on the order of" $$n$$ operations.
25
41
Unfortunately, at this point, these notations might be a little vague.
26
42
In fact, it was incredibly vague for me for a long time, and it wasn't until I saw the notations in action that it all started to make sense, so that's what this section is about: providing concrete examples to better understand computational complexity notation.
27
43
44
+
######In algorithms below, let us consider that the *slowest* statement is `println`, and we always talk about all the `println` in the function.
45
+
28
46
## Constant Time
29
47
30
48
Let's write some code that reads in an array of length `n` and runs with constant time:
@@ -137,9 +155,10 @@ Here is a simple example of a function with exponential runtime:
137
155
# Here, n is the number of iterations
138
156
functionexponential(value::Int64, n::Int64)
139
157
println(value)
140
-
value +=1
141
-
exponential(value, n-1)
142
-
exponential(value, n-1)
158
+
if(n >=0)
159
+
value +=1
160
+
exponential(value, n-1)
161
+
exponential(value, n-1)
143
162
end
144
163
```
145
164
@@ -152,16 +171,15 @@ Instead of taking a value and computing more and more values each time, a good e
152
171
# Here, cutoff is an arbitrary variable to know when to stop recursing
0 commit comments