What's an algorithm? - David J. Malan

2,570,249 views ・ 2013-05-20

TED-Ed


Dvaput kliknite na engleske titlove ispod za reprodukciju videozapisa.

00:00
Translator: Andrea McDonough Reviewer: Jessica Ruby
0
0
7000
Prevoditelj: Neda Vrkic Recezent: Sanda L
00:15
What's an algorithm?
1
15483
1348
Što je algoritam?
00:16
In computer science,
2
16831
831
U informatici,
00:17
an algorithm is a set of instructions
3
17662
1754
algoritam je skup uputa
00:19
for solving some problem, step-by-step.
4
19416
2689
za rješavanje nekog problema, korak-po-korak.
00:22
Typically, algorithms are executed by computers,
5
22105
2272
U pravilu, algoritme izvršavaju računala,
00:24
but we humans have algorithms as well.
6
24377
2167
ali i mi ljudi imamo algoritme.
00:26
For instance, how would you go about counting
7
26544
1853
Na primjer, kako biste izbrojali
00:28
the number of people in a room?
8
28397
1820
broj ljudi u prostoriji?
00:30
Well, if you're like me,
9
30217
1215
Pa, ako ste kao ja,
00:31
you probably point at each person,
10
31432
1496
vjerojatno biste pokazali na svaku osobu,
00:32
one at a time,
11
32928
960
00:33
and count up from 0:
12
33888
1837
jednu po jednu,
i brojali od 0:
00:35
1, 2, 3, 4 and so forth.
13
35725
2873
1, 2, 3, 4 i tako dalje.
00:38
Well, that's an algorithm.
14
38598
1113
Dakle, to je algoritam.
00:39
In fact, let's try to express it
15
39711
1134
Zapravo, probajmo to izraziti
00:40
a bit more formally in pseudocode,
16
40845
2229
malo formalnije u pseudokodu,
sintaksi sličnoj engleskom
00:43
English-like syntax
17
43074
831
00:43
that resembles a programming language.
18
43905
2124
koja nalikuje na programski jezik.
00:46
Let n equal 0.
19
46029
1767
Neka je n jednako 0.
00:47
For each person in room, set n = n + 1.
20
47796
4792
Za svaku osobu u prostoriji, skup n = n + 1.
00:52
How to interpret this pseudocode?
21
52588
1497
Kako protumačiti ovaj pseudokod?
00:54
Well, line 1 declares, so to speak,
22
54085
1836
Pa, redak 1 izjavljuje, tako reći,
00:55
a variable called n
23
55921
1416
varijablu nazvanu n
00:57
and initializes its value to zero.
24
57337
2379
i započinje svoju vrijednost na 0.
00:59
This just means that at the beginning of our algorithm,
25
59716
2335
Ovo samo znači da na početku našeg algoritma,
01:02
the thing with which we're counting
26
62051
1584
stvar s kojom računamo
01:03
has a value of zero.
27
63635
1668
ima vrijednost nula.
01:05
After all, before we start counting,
28
65303
1336
Nakon svega, prije nego počnemo brojati,
01:06
we haven't counted anything yet.
29
66639
1669
nismo još ništa izbrojali.
01:08
Calling this variable n is just a convention.
30
68308
2248
Nazivanje ove varijable n je samo dogovor.
01:10
I could have called it almost anything.
31
70556
2005
Mogao sam je nazvati bilo kako.
01:12
Now, line 2 demarks the start of loop,
32
72561
2086
Sada, redak 2 označava početak petlje,
01:14
a sequence of steps that will repeat some number of times.
33
74647
3044
slijed koraka koji će se ponoviti nekoliko puta.
01:17
So, in our example, the step we're taking
34
77691
1794
Pa, u našem primjeru, korak koji poduzimamo
01:19
is counting people in the room.
35
79485
1734
brojanje je ljudi u prostoriji.
01:21
Beneath line 2 is line 3,
36
81219
1763
Ispod retka 2 je redak 3,
01:22
which describes exactly how we'll go about counting.
37
82982
2511
što točno opisuje kako ćemo brojati.
01:25
The indentation implies that it's line 3
38
85493
2250
Uvlačenje podrazumijeva da će se redak 3
01:27
that will repeat.
39
87743
1222
ponavljati.
01:28
So, what the pseudocode is saying
40
88965
1219
Dakle, što pseudokod govori
01:30
is that after starting at zero,
41
90184
2066
je da nakon kretanja od nule,
01:32
for each person in the room,
42
92250
1710
za svaku osobu u prostoriji,
01:33
we'll increase n by 1.
43
93960
2218
n ćemo povećati za 1.
01:36
Now, is this algorithm correct?
44
96178
2329
Sada, je li ovaj algoritam ispravan?
01:38
Well, let's bang on it a bit.
45
98507
1608
Pa, ajmo malo to razbiti.
01:40
Does it work if there are 2 people in the room?
46
100115
2826
Funkcionira li to ako su dvije osobe u prostoriji?
01:42
Let's see.
47
102941
780
Da vidimo.
01:43
In line 1, we initialize n to zero.
48
103721
2085
U retku 1, počinjemo s n od nule.
01:45
For each of these two people,
49
105806
1302
Za svaku od ove dvije osobe
01:47
we then increment n by 1.
50
107108
2168
povećamo onda n za 1.
01:49
So, in the first trip through the loop,
51
109276
1582
Dakle, u prvom krugu petlje
01:50
we update n from zero to 1,
52
110858
2005
ažuriramo n od nule na 1.
01:52
on the second trip through that same loop,
53
112863
1655
u drugom krugu kroz istu petlju,
01:54
we update n from 1 to 2.
54
114518
2249
ažuriramo n od 1 na 2.
01:56
And so, by this algorithm's end, n is 2,
55
116767
3341
I tako, do kraja ovog algoritma, n je 2,
02:00
which indeed matches the number of people in the room.
56
120108
2113
što doista odgovara broju ljudi u prostoriji.
02:02
So far, so good.
57
122221
1223
Zasada je dobro.
02:03
How about a corner case, though?
58
123444
1752
A što je onda s izuzetkom?
02:05
Suppose that there are zero people in the room,
59
125196
1964
Pretpostavimo da je nula osoba u prostoriji,
02:07
besides me, who's doing the counting.
60
127160
2347
osim mene, koji broji.
02:09
In line 1, we again initialize n to zero.
61
129507
3010
U retku 1 opet počinjemo s n od nule.
02:12
This time, though, line 3 doesn't execute at all
62
132517
2522
Ovaj put, međutim, redak 3 uopće se ne izvršava
02:15
since there isn't a person in the room,
63
135039
1880
budući da u prostoriji nema nikoga,
02:16
and so, n remains zero,
64
136919
1624
i tako, n ostaje nula,
02:18
which indeed matches the number of people in the room.
65
138543
2359
što doista odgovara broju osoba u prostoriji.
02:20
Pretty simple, right?
66
140902
906
Prilično jednostavno, zar ne?
02:21
But counting people one a time is pretty inefficient, too, no?
67
141808
3216
Ali brojanje ljudi jedan po jedan je također prilično neučinkovito, zar ne?
02:25
Surely, we can do better!
68
145024
1568
Sigurno možemo bolje!
02:26
Why not count two people at a time?
69
146592
1866
Zašto ne brojati dvije osobe odjednom?
02:28
Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth,
70
148458
5099
Umjesto brojanja 1, 2, 3, 4, 5, 6, 7, 8 i tako dalje,
02:33
why not count
71
153557
918
zašto ne brojati
02:34
2, 4, 6, 8, and so on?
72
154475
2127
2, 4, 6, 8 i tako dalje?
02:36
It even sounds faster, and it surely is.
73
156602
2418
Čak i zvuči brže, i sigurno jest.
02:39
Let's express this optimization in pseudocode.
74
159020
2835
Izrazimo ovu optimizaciju u pseudokodu.
02:41
Let n equal zero.
75
161855
1462
Neka je n jednako nula.
02:43
For each pair of people in room,
76
163317
1997
Za svaki par osoba u prostoriji,
02:45
set n = n + 2.
77
165314
2588
skup n = n + 2.
02:47
Pretty simple change, right?
78
167902
1673
Prilično jednostavna promjena, zar ne?
02:49
Rather than count people one at a time,
79
169575
1791
Umjesto da brojimo osobe jednu po jednu,
02:51
we instead count them two at a time.
80
171366
2343
brojimo ih po dvije odjednom.
02:53
This algorithm's thus twice as fast as the last.
81
173709
2815
Ovaj algoritam je stoga duplo brži od prošlog.
02:56
But is it correct?
82
176524
1346
Ali je li točan?
02:57
Let's see.
83
177870
794
Da vidimo.
02:58
Does it work if there are 2 people in the room?
84
178664
2125
Funkcionira li ako su u prostoriji dvije osobe?
03:00
In line 1, we initialize n to zero.
85
180789
2342
U retku 1, počinjemo s n od nule.
03:03
For that one pair of people, we then increment n by 2.
86
183131
3268
Za taj jedan par osoba, povećamo tada n za 2.
03:06
And so, by this algorithm's end, n is 2,
87
186399
2522
I tako, do kraja algoritma, n je 2,
03:08
which indeed matches the number of people in the room.
88
188921
2382
što doista odgovara broju osoba u prostoriji.
03:11
Suppose next that there are zero people in the room.
89
191303
2412
Pretpostavimo sljedeće, da je nula osoba u prostoriji.
03:13
In line 1, we initialize n to zero.
90
193715
2587
U retku 1, počinjemo s n od nule.
03:16
As before, line 3 doesn't execute at all
91
196302
2080
Kao prije, redak 3 se uopće ne izvršava
03:18
since there aren't any pairs of people in the room,
92
198382
2342
s obzirom da u prostoriji nema parova osoba,
03:20
and so, n remains zero,
93
200724
1686
i tako, n ostaje nula,
03:22
which indeed matches the number of people in the room.
94
202410
2773
što doista odgovara broju osoba u prostoriji.
03:25
But what if there are 3 people in the room?
95
205183
2372
Ali što ako su tri osobe u prostoriji?
03:27
How does this algorithm fair?
96
207555
1937
Kako se ovaj algoritam slaže?
03:29
Let's see.
97
209492
829
Da vidimo.
03:30
In line 1, we initialize n to zero.
98
210321
2670
U retku 1, počinjemo s n od nule.
03:32
For a pair of those people,
99
212991
1290
Za par tih osoba
03:34
we then increment n by 2,
100
214281
1916
tada povećamo n za 2,
03:36
but then what?
101
216197
992
ali što onda?
03:37
There isn't another full pair of people in the room,
102
217189
2262
U prostoriji nema još jedan puni par osoba
03:39
so line 2 no longer applies.
103
219451
2210
pa se redak 2 više ne primjenjuje.
03:41
And so, by this algorithm's end,
104
221661
1669
I tako, do kraja algoritma,
03:43
n is still 2, which isn't correct.
105
223330
2596
n je još 2, što nije ispravno.
03:45
Indeed this algorithm is said to be buggy
106
225926
2332
Doista se za ovaj algoritam kaže da je neispravan
03:48
because it has a mistake.
107
228258
1148
jer ima grešku.
03:49
Let's redress with some new pseudocode.
108
229406
1896
Ispravimo se nekim novim pseudokodom.
03:51
Let n equal zero.
109
231302
1793
Neka je n jednak nuli.
03:53
For each pair of people in room,
110
233095
2123
Za svaki par osoba u prostoriji,
03:55
set n = n + 2.
111
235218
2422
skup n = n + 2.
03:57
If 1 person remains unpaired,
112
237640
2459
Ako jedna osoba ostane bez para,
04:00
set n = n + 1.
113
240099
2376
skup n = n + 1.
04:02
To solve this particular problem,
114
242475
1507
Da bismo riješili ovaj poseban problem,
04:03
we've introduced in line 4 a condition,
115
243982
2249
u redak 4 smo uveli uvjet,
04:06
otherwise known as a branch,
116
246231
1835
inače poznat kao grana,
04:08
that only executes if there is one person
117
248066
2253
koja se izvršava samo ako postoji jedna osoba
04:10
we could not pair with another.
118
250319
1876
koju ne možemo spariti s drugom.
04:12
So now, whether there's 1 or 3
119
252195
2065
Pa sad, bilo da postoji 1 ili 3
04:14
or any odd number of people in the room,
120
254260
2273
ili bilo koji neparan broj osoba u prostoriji,
04:16
this algorithm will now count them.
121
256533
2288
ovaj će ih algoritam sada brojati.
04:18
Can we do even better?
122
258821
1345
Možemo li još bolje?
04:20
Well, we could count in 3's or 4's or even 5's and 10's,
123
260166
3294
Pa, mogli bismo brojati po 3 ili 4, ili čak 5 i 10,
04:23
but beyond that it's going to get
124
263460
1300
ali osim toga to će biti
04:24
a little bit difficult to point.
125
264760
1870
malo teško za naznačiti.
04:26
At the end of the day,
126
266630
937
Na kraju dana,
04:27
whether executed by computers or humans,
127
267567
2264
bilo da ih izvršavaju računala ili ljudi,
04:29
algorithms are just a set of instructions
128
269831
1960
algoritmi su samo skup uputa
04:31
with which to solve problems.
129
271791
1838
kojima se rješavaju problemi.
04:33
These were just three.
130
273629
1743
Ovo su bila samo tri.
04:35
What problem would you solve with an algorithm?
131
275372
2982
Koji biste vi problem riješili algoritmom?
O ovoj web stranici

Ova stranica će vas upoznati s YouTube videozapisima koji su korisni za učenje engleskog jezika. Vidjet ćete lekcije engleskog koje vode vrhunski profesori iz cijelog svijeta. Dvaput kliknite na engleske titlove prikazane na svakoj video stranici da biste reproducirali video s tog mjesta. Titlovi se pomiču sinkronizirano s reprodukcijom videozapisa. Ako imate bilo kakvih komentara ili zahtjeva, obratite nam se putem ovog obrasca za kontakt.

https://forms.gle/WvT1wiN1qDtmnspy7