Skip to content

Commit 57a4ca6

Browse files
ClémentClément
authored andcommitted
Added notes on complexity.
1 parent 3301486 commit 57a4ca6

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

source/code/snippets/complexity.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.IO;
3+
4+
class Program
5+
{
6+
static int factorial(int n)
7+
{
8+
if (n == 0)
9+
return 1;
10+
else
11+
return checked(factorial(n - 1) * n);
12+
}
13+
14+
static void Main()
15+
{
16+
Console.WriteLine(int.MaxValue);
17+
18+
int x = int.MaxValue;
19+
int y = x + 1;
20+
Console.WriteLine(y);
21+
22+
try
23+
{
24+
y = checked(x + 1);
25+
}
26+
catch (System.OverflowException)
27+
{
28+
Console.WriteLine("An overflow exception was thrown.");
29+
}
30+
31+
int count0 = 0;
32+
int n0 = 3;
33+
try
34+
{
35+
while (true)
36+
{
37+
count0++;
38+
n0 = checked(n0 +1 );
39+
}
40+
}
41+
catch (System.OverflowException)
42+
{
43+
Console.WriteLine("An overflow exception was thrown after " + count0 + " iterations.");
44+
// An overflow exception was thrown after 2147483645 iterations.
45+
}
46+
47+
int count1 = 0;
48+
int n1 = 3;
49+
try
50+
{
51+
while (true)
52+
{
53+
count1++;
54+
n1 = checked(n1 * n1);
55+
}
56+
}
57+
catch (System.OverflowException)
58+
{
59+
Console.WriteLine("An overflow exception was thrown after " + count1 + " iterations.");
60+
// An overflow exception was thrown after 5 iterations.
61+
}
62+
63+
int count2 = 0;
64+
int n2 = 3;
65+
try
66+
{
67+
while (true)
68+
{
69+
count2++;
70+
n2 = factorial(n2);
71+
}
72+
}
73+
catch (System.OverflowException)
74+
{
75+
Console.WriteLine("An overflow exception was thrown after " + count2 + " iterations.");
76+
// An overflow exception was thrown after 3 iterations.
77+
}
78+
79+
int count3 = 0;
80+
int n3 = 100;
81+
try
82+
{
83+
while (true)
84+
{
85+
count3++;
86+
n3 = checked(n3 * (int)Math.Log(n3, 2));
87+
}
88+
}
89+
catch (System.OverflowException)
90+
{
91+
Console.WriteLine("An overflow exception was thrown after " + count3 + " iterations.");
92+
// An overflow exception was thrown after 7 iterations.
93+
}
94+
}
95+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Some Notes on Complexity
2+
3+
Have a look at the [Big-O complexity chart](https://www.bigocheatsheet.com/):
4+
5+
![Big-O Complexity Chart](https://www.bigocheatsheet.com/img/big-o-complexity-chart.png)
6+
7+
A function [has an order](https://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions), it can be for example
8+
9+
- constant (O(c)),
10+
- logarithmic (O(log n)),
11+
- linear (O(n)),
12+
- [linearithmic](https://en.wikipedia.org/wiki/Time_complexity#Quasilinear_time) (O(n log n)),
13+
- quadratic (O(n^2)),
14+
- cubic (O(n^3)),
15+
- exponential (O(c^n)),
16+
- factorial (O(n!)).
17+
18+
This can make a *very* significant difference, as exemplified in the following code:
19+
20+
```
21+
!include code/snippets/complexity.cs
22+
```

source/order

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
./docs/programming_and_computer_usage/keyboard_shortcuts.md
1616
./docs/programming_and_computer_usage/datatypes_in_csharp.md
1717
./docs/programming_and_computer_usage/uml_class.md
18+
./docs/programming_and_computer_usage/complexity.md
1819
./lectures/
1920
./lectures/intro/
2021
./lectures/intro/computers_and_programming.md

0 commit comments

Comments
 (0)