Skip to content

Commit fd23e44

Browse files
committed
Formatting of algorithms markdown
1 parent ca893ff commit fd23e44

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

00 Algorithms/2 Thinking in algorithms.md

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OUTPUT:
2727

2828
As you can see, we have used **labels** `a` and `b` for an input data that <u>needs to be provided at the program execution</u> ( at the moment, let's do not worry how) and also we have performed some computations and **labeled** (named) their result as `area`. Finally, we can provide this named value as the algorithm output.
2929

30-
Let's take a look at another example. Given two numbers *x*<sub>1</sub> and *x*<sub>2</sub> computer their average. The algorithm would be as follows:
30+
Let's take a look at another example. Given two numbers *x* and *x* computer their average. The algorithm would be as follows:
3131

3232
```
3333
INPUT:
@@ -43,12 +43,12 @@ OUTPUT:
4343

4444
What you see above is a recipe. It contains two simple steps. You should try to track it and see how the labeled values change:
4545

46-
| STEP | *x*<sub>1</sub> | *x*<sub>2</sub> | *total* | *average* | RESULT |
47-
| -------------------------------------------- | --------------- | --------------- | ------- | --------- | ------ |
48-
| **INPUT** | **2** | **4** | | | |
49-
| *total* = *x*<sub>1</sub> + *x*<sub>2</sub>. | 2 | 4 | 6 | | |
50-
| *average* = *total* / 2. | 2 | 4 | 6 | 3 | |
51-
| **OUTPUT** | 2 | 4 | 6 | 3 | **3** |
46+
| STEP | *x*| *x* | *total* | *average* | RESULT |
47+
| ------------------------ | ----- | ----- | ------- | --------- | ------ |
48+
| **INPUT** | **2** | **4** | | | |
49+
| *total* = *x* + *x*₂. | 2 | 4 | 6 | | |
50+
| *average* = *total* / 2. | 2 | 4 | 6 | 3 | |
51+
| **OUTPUT** | 2 | 4 | 6 | 3 | **3** |
5252

5353
Now, something more complicated: given two numbers, tell what is the value of the larger ones:
5454

@@ -66,25 +66,25 @@ OUTPUT:
6666

6767
Let's track it for some sample data:
6868

69-
| STEP | *x*<sub>1</sub> | *x*<sub>2</sub> | *larger* | RESULT |
70-
| --------------------------------------------- | --------------- | --------------- | -------- | ------ |
71-
| **INPUT** | **2** | **1** | | |
72-
| Checking if *x*<sub>1</sub> > *x*<sub>2</sub> | 2 | 1 | | yes |
73-
| *larger* = *x*<sub>1</sub> | 2 | 1 | 2 | |
74-
| **OUTPUT** | 2 | 1 | 2 | **2** |
69+
| STEP | *x*| *x* | *larger* | RESULT |
70+
| ----------------------- | ----- | ----- | -------- | ------ |
71+
| **INPUT** | **2** | **1** | | |
72+
| Checking if *x* > *x* | 2 | 1 | | yes |
73+
| *larger* = *x* | 2 | 1 | 2 | |
74+
| **OUTPUT** | 2 | 1 | 2 | **2** |
7575

7676
How will it look for some different data?
7777

78-
| STEP | *x*<sub>1</sub> | *x*<sub>2</sub> | *larger* | RESULT |
79-
| --------------------------------------------- | --------------- | --------------- | -------- | ------ |
80-
| **INPUT** | **2** | **3** | | |
81-
| Checking if *x*<sub>1</sub> > *x*<sub>2</sub> | 2 | 3 | | no |
82-
| *larger* = *x*<sub>2</sub> | 2 | 3 | 3 | |
83-
| **OUTPUT** | 2 | 3 | 3 | **3** |
78+
| STEP | *x*| *x* | *larger* | RESULT |
79+
| ----------------------- | ----- | ----- | -------- | ------ |
80+
| **INPUT** | **2** | **3** | | |
81+
| Checking if *x* > *x* | 2 | 3 | | no |
82+
| *larger* = *x* | 2 | 3 | 3 | |
83+
| **OUTPUT** | 2 | 3 | 3 | **3** |
8484

8585
The above algorithm was not performed linearly. Depending on some condition either one or another operation was performed. This is called a *conditional*.
8686

87-
Let's take a look at more advanced example — solution to [Quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation) of the form *a* *x*<sup>2</sup> + *b* *x* + *c* = 0. I hope you remember how to do it! Hence, the simplest and most naive algorithm would be:
87+
Let's take a look at more advanced example — solution to [Quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation) of the form *a* *x*² + *b* *x* + *c* = 0. I hope you remember how to do it! Hence, the simplest and most naive algorithm would be:
8888

8989
```bash
9090
INPUT:
@@ -122,58 +122,58 @@ OUTPUT:
122122
x₁, x₂
123123
```
124124

125-
Above, you can see that the algorithm can take three paths, depending on the value of *Δ*. What is important, is that in each path we mention both *x*<sub>1</sub> and *x*<sub>2</sub>. This is because, we have identified both of them as an output, so **we must define such names in every possible path!** Even in the cases when one or both of them does not exits, we mark them as non-existent (right now, you need not worry how: programming languages allow to do it one way or another).
125+
Above, you can see that the algorithm can take three paths, depending on the value of *Δ*. What is important, is that in each path we mention both *x* and *x*. This is because, we have identified both of them as an output, so **we must define such names in every possible path!** Even in the cases when one or both of them does not exits, we mark them as non-existent (right now, you need not worry how: programming languages allow to do it one way or another).
126126

127127
So the algorithm looks good... Let's track if for some data:
128128

129-
Solving *x*<sup>2</sup> + 2 *x* – 8 = 0:
130-
131-
| STEP | *a* | *b* | *c* | *Δ* | *x*<sub>1</sub> | *x*<sub>2</sub> | RESULT |
132-
| ----------------------------------------- | ----- | ----- | ------ | --- | --------------- | --------------- | ------------- |
133-
| **INPUT** | **1** | **2** | **-8** | | | | |
134-
| *Δ* = *b*<sup>2</sup> – 4×*a*×*c* | 1 | 2 | -8 | 36 | | | |
135-
| Checking if *Δ* > 0 | 1 | 2 | -8 | 36 | | | yes |
136-
| *x*<sub>1</sub> = (–*b* – √*Δ*) / (2×*a*) | 1 | 2 | -8 | 36 | -4 | | |
137-
| *x*<sub>2</sub> = (–*b* + √*Δ*) / (2×*a*) | 1 | 2 | -8 | 36 | -4 | 2 | |
138-
| **OUTPUT** | 1 | 2 | -8 | 36 | -4 | 2 | **-4**, **2** |
139-
140-
Solving *x*<sup>2</sup> – 2 *x* + 1 = 0:
141-
142-
| STEP | *a* | *b* | *c* | *Δ* | *x*<sub>1</sub> | *x*<sub>2</sub> | RESULT |
143-
| --------------------------------- | ----- | ------ | ----- | --- | --------------- | --------------- | ---------------- |
144-
| **INPUT** | **1** | **-2** | **1** | | | | |
145-
| *Δ* = *b*<sup>2</sup> – 4×*a*×*c* | 1 | -2 | 1 | 0 | | | |
146-
| Checking if *Δ* > 0 | 1 | -2 | 1 | 0 | | | no |
147-
| Checking if *Δ* = 0 | 1 | -2 | 1 | 0 | | | yes |
148-
| *x*<sub>1</sub> = –*b* / (2×*a*) | 1 | -2 | 1 | 0 | 1 | | |
149-
| *x*<sub>2</sub> does not exist | 1 | -2 | 1 | 0 | 1 | none | |
150-
| **OUTPUT** | 1 | -2 | 1 | 0 | 1 | none | **-4**, **none** |
151-
152-
Solving *x*<sup>2</sup> + 2 *x* + 3 = 0:
153-
154-
| STEP | *a* | *b* | *c* | *Δ* | *x*<sub>1</sub> | *x*<sub>2</sub> | RESULT |
155-
| ----------------------------------------------- | --- | --- | --- | --- | --------------- | --------------- | ------------------ |
156-
| **INPUT** |**1**|**2**|**3**| | | | |
157-
| *Δ* = *b*<sup>2</sup> – 4×*a*×*c* | 1 | 2 | 3 | 0 | | | |
158-
| Checking if *Δ* > 0 | 1 | 2 | 3 | 0 | | | no |
159-
| Checking if *Δ* = 0 | 1 | 2 | 3 | 0 | | | no |
160-
| *x*<sub>1</sub> and *x*<sub>2</sub> don't exist | 1 | 2 | 3 | 0 | none | none | |
161-
| **OUTPUT** | 1 | 2 | 3 | 0 | none | none | **none**, **none** |
129+
Solving *x*² + 2 *x* – 8 = 0:
130+
131+
| STEP | *a* | *b* | *c* | *Δ* | *x* | *x* | RESULT |
132+
| ------------------------------ | ----- | ----- | ------ | --- | ---- | ---- | ------------- |
133+
| **INPUT** | **1** | **2** | **-8** | | | | |
134+
| *Δ* = *b*² – 4×*a*×*c* | 1 | 2 | -8 | 36 | | | |
135+
| Checking if *Δ* > 0 | 1 | 2 | -8 | 36 | | | yes |
136+
| *x* = (–*b* – √*Δ*) / (2×*a*) | 1 | 2 | -8 | 36 | -4 | | |
137+
| *x* = (–*b* + √*Δ*) / (2×*a*) | 1 | 2 | -8 | 36 | -4 | 2 | |
138+
| **OUTPUT** | 1 | 2 | -8 | 36 | -4 | 2 | **-4**, **2** |
139+
140+
Solving *x*² – 2 *x* + 1 = 0:
141+
142+
| STEP | *a* | *b* | *c* | *Δ* | *x* | *x* | RESULT |
143+
| ---------------------- | ----- | ------ | ----- | --- | ---- | ---- | ---------------- |
144+
| **INPUT** | **1** | **-2** | **1** | | | | |
145+
| *Δ* = *b*² – 4×*a*×*c* | 1 | -2 | 1 | 0 | | | |
146+
| Checking if *Δ* > 0 | 1 | -2 | 1 | 0 | | | no |
147+
| Checking if *Δ* = 0 | 1 | -2 | 1 | 0 | | | yes |
148+
| *x* = –*b* / (2×*a*) | 1 | -2 | 1 | 0 | 1 | | |
149+
| *x* does not exist | 1 | -2 | 1 | 0 | 1 | none | |
150+
| **OUTPUT** | 1 | -2 | 1 | 0 | 1 | none | **-4**, **none** |
151+
152+
Solving *x*² + 2 *x* + 3 = 0:
153+
154+
| STEP | *a* | *b* | *c* | *Δ* | *x* | *x* | RESULT |
155+
| ------------------------- | ----- | ----- | ----- | --- | ---- | ---- | ------------------ |
156+
| **INPUT** | **1** | **2** | **3** | | | | |
157+
| *Δ* = *b*² – 4×*a*×*c* | 1 | 2 | 3 | 0 | | | |
158+
| Checking if *Δ* > 0 | 1 | 2 | 3 | 0 | | | no |
159+
| Checking if *Δ* = 0 | 1 | 2 | 3 | 0 | | | no |
160+
| *x* and *x* don't exist | 1 | 2 | 3 | 0 | none | none | |
161+
| **OUTPUT** | 1 | 2 | 3 | 0 | none | none | **none**, **none** |
162162

163163
Let's consider another case: *a* = 0, *b* = 2, *c* = 3:
164164

165-
| STEP | *a* | *b* | *c* | *Δ* | *x*<sub>1</sub> | *x*<sub>2</sub> | RESULT |
166-
| ----------------------------------------- | ----- | ----- | ----- | --- | -------------------------- | --------------- | ------ |
167-
| **INPUT** | **0** | **2** | **3** | | | | |
168-
| *Δ* = *b*<sup>2</sup> – 4×*a*×*c* | 0 | 2 | 3 | 4 | | | |
169-
| Checking if *Δ* > 0 | 0 | 2 | 3 | 4 | | | yes |
170-
| *x*<sub>1</sub> = (–*b* – √*Δ*) / (2×*a*) | 0 | 2 | 3 | 4 | <span style="color: red">0/0</span> | | |
165+
| STEP | *a* | *b* | *c* | *Δ* | *x* | *x* | RESULT |
166+
| ------------------------------ | ----- | ----- | ----- | --- | ----------------------------------- | ---- | ------ |
167+
| **INPUT** | **0** | **2** | **3** | | | | |
168+
| *Δ* = *b*² – 4×*a*×*c* | 0 | 2 | 3 | 4 | | | |
169+
| Checking if *Δ* > 0 | 0 | 2 | 3 | 4 | | | yes |
170+
| *x* = (–*b* – √*Δ*) / (2×*a*) | 0 | 2 | 3 | 4 | <span style="color: red">0/0</span> | | |
171171

172172
Can you see what happened? Out algorithm failed for a case where *a* = 0! What can be done about it? Well, we need to explicitly check this case. So the algorithm will be:
173173

174174
```
175175
INPUT:
176-
a, b, c — eqution coeffi ients.
176+
a, b, c — eqution coefficients.
177177
178178
STEPS:
179179
— Check if a ≠ 0, if so:
@@ -196,7 +196,7 @@ OUTPUT:
196196

197197
What you can see above is a **nested conditional**. First we check one condition (*a* ≠ 0) and in one of the cases we check another one (*Δ* > 0 or *Δ* = 0). When you write your own algorithms, clearly mark which block is nested in which!
198198

199-
Please trace this algorithm for the equations *x*<sup>2</sup> + 2 *x* – 8 = 0, *x*<sup>2</sup> – 2 *x* + 1 = 0, *x*<sup>2</sup> + 2 *x* + 3 = 0, and 2 *x* + 4 = 0 (*a* = 0) yourself.
199+
Please trace this algorithm for the equations *x*²&nbsp;+&nbsp;2&nbsp;*x*&nbsp;–8&nbsp;=&nbsp;0, *x*²&nbsp;&nbsp;2*x*&nbsp;+&nbsp;1&nbsp;=&nbsp;0, *x*²&nbsp;+&nbsp;2&nbsp;*x*&nbsp;+&nbsp;3&nbsp;=&nbsp;0, and 2&nbsp;*x*&nbsp;+&nbsp;4&nbsp;=&nbsp;0 (*a*&nbsp;=&nbsp;0) yourself.
200200

201201
## Summary
202202

0 commit comments

Comments
 (0)