# What’s an algorithm? – David J. Malan

Articles, Blog 100 Comments

Translator: Andrea McDonough

Reviewer: Jessica Ruby What’s an algorithm? In computer science, an algorithm is a set of instructions for solving some problem, step-by-step. Typically, algorithms are executed by computers, but we humans have algorithms as well. For instance, how would you go about counting the number of people in a room? Well, if you’re like me, you probably point at each person, one at a time, and count up from 0: 1, 2, 3, 4 and so forth. Well, that’s an algorithm. In fact, let’s try to express it a bit more formally in pseudocode, English-like syntax that resembles a programming language. Let n equal 0. For each person in room, set n=n + 1. How to interpret this pseudocode? Well, line 1 declares, so to speak, a variable called n and initializes its value to zero. This just means that at the beginning of our algorithm, the thing with which we’re counting has a value of zero. After all, before we start counting, we haven’t counted anything yet. Calling this variable n is just a convention. I could have called it almost anything. Now, line 2 demarks the start of loop, a sequence of steps that will repeat some number of times. So, in our example, the step we’re taking is counting people in the room. Beneath line 2 is line 3, which describes exactly how we’ll go about counting. The indentation implies that it’s line 3 that will repeat. So, what the pseudocode is saying is that after starting at zero, for each person in the room, we’ll increase n by 1. Now, is this algorithm correct? Well, let’s bang on it a bit. Does it work if there are 2 people in the room? Let’s see. In line 1, we initialize n to zero. For each of these two people, we then increment n by 1. So, in the first trip through the loop, we update n from zero to 1, on the second trip through that same loop, we update n from 1 to 2. And so, by this algorithm’s end, n is 2, which indeed matches the number of people in the room. So far, so good. How about a corner case, though? Suppose that there are zero people in the room, besides me, who’s doing the counting. In line 1, we again initialize n to zero. This time, though, line 3 doesn’t execute at all since there isn’t a person in the room, and so, n remains zero, which indeed matches the number of people in the room. Pretty simple, right? But counting people one a time is pretty inefficient, too, no? Surely, we can do better! Why not count two people at a time? Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth, why not count 2, 4, 6, 8, and so on? It even sounds faster, and it surely is. Let’s express this optimization in pseudocode. Let n equal zero. For each pair of people in room, set n=n + 2. Pretty simple change, right? Rather than count people one at a time, we instead count them two at a time. This algorithm’s thus twice as fast as the last. But is it correct? Let’s see. Does it work if there are 2 people in the room? In line 1, we initialize n to zero. For that one pair of people, we then increment n by 2. And so, by this algorithm’s end, n is 2, which indeed matches the number of people in the room. Suppose next that there are zero people in the room. In line 1, we initialize n to zero. As before, line 3 doesn’t execute at all since there aren’t any pairs of people in the room, and so, n remains zero, which indeed matches the number of people in the room. But what if there are 3 people in the room? How does this algorithm fair? Let’s see. In line 1, we initialize n to zero. For a pair of those people, we then increment n by 2, but then what? There isn’t another full pair of people in the room, so line 2 no longer applies. And so, by this algorithm’s end, n is still 2, which isn’t correct. Indeed this algorithm is said to be buggy because it has a mistake. Let’s redress with some new pseudocode. Let n equal zero. For each pair of people in room, set n=n + 2. If 1 person remains unpaired, set n=n + 1. To solve this particular problem, we’ve introduced in line 4 a condition, otherwise known as a branch, that only executes if there is one person we could not pair with another. So now, whether there’s 1 or 3 or any odd number of people in the room, this algorithm will now count them. Can we do even better? Well, we could count in 3’s or 4’s or even 5’s and 10’s, but beyond that it’s going to get a little bit difficult to point. At the end of the day, whether executed by computers or humans, algorithms are just a set of instructions with which to solve problems. These were just three. What problem would you solve with an algorithm?

Leevian chenPost authorwhy is the expression n=n+1 and not n=n2 or n=n1 for the people

Bienne TAMPost authoreasy to understand and apply, unlike many other programs, too complicated!!!

Nishit PaulPost authorSo this is from cs50.

Wandering TravelerPost authorThis video made no sense. If N=0 then we go to an equation of N=N+1? That first N should be a different letter. Is the 2nd N not the same as the 1st N? Is the first N the result/answer and the 2nd N stands for "number"?

Tanut ChankasemPost authorAlgorithms are a set of of instructions to solve problems that executed by computers or human. Then, am I programmed to wrote this comment to solve the problem that I wanted to do so by typing this on the keyboard ? What if we were written with a super complex algorithm. the code is so complex and it is infinite. So, who wrote that set of instruction?

Gillian Chloe PePost authorhttps://youtu.be/hqV0pMLv54o

pls like

subscribe

i will do the same

Roshan JafriPost authorin the second method for the loop N would be equal to 6, for(each $people){N += 2;}so it runs atleast thrice and end at 6 2 x 3 = 6

yAlexDePost authormy fav algorithm R U R' U' (rubik's bube)

Zaylan SealsPost authorThank you sooo much I’m going to start getting a BS in the fall of 2020 and I hear everyone saying algorithms are hard but watching you make it look basic just makes me feel a lot more confident to take on Computer science

Anand VishwakarmaPost authorAwesomemational explation

Raihan AkramPost authorLast music was just awesome!

Fandika IkhsanPost authorMalan from harvard?!

Craig PoPost authorAlgorithm

is an anagram for

Logarithm

Now i'm confused

is it the other way around?

aaaaaaaAAAAAAعبدالعزيزPost authorيا اخي ترجم

Jake PattersonPost authorSo it’s like a mathematical series?

Ali HodrojPost authorVery useful and straight to the point. 🙂

Dack GibsonPost author3:30 That one third wheel.

Dack GibsonPost author3:30 That one third wheel.

Mistah FTM 7394Post authoralogrithms is use in attempts to socially influence ppl to disassociate themselves. I'm Single Proud Independent Dreadhead Dark Skin Hetero FTM, & your alogrithms send MI things that don't affirm MI. do y'all think MI weak like trolls/perps/gangstalkers/pawns that y'all control. Always a leader & not a follower. Y'all stop at satanic/demonic influence, MI go way pass that & yet y'all attempting to burden MI with rumors, thinking MI surrender to your lies about MI. Y'all want MI to die slow. Well MI gunna kill y'all faster.

Ashutosh SinghPost authorawesome way to explain algorithms. Loved it.

Tushar BhaleraoPost authorNow i understood algorithm …better than college

Pranav GhandadePost author4:39 how to always get your comment on top.

JevilPost authorI LOVE LOGIC STUFF!!!!!!!!1!!!!!11eleventy1

The Indian CreatorsPost authorSee this what is meaning of algorithm? How to write an algorithm? https://youtu.be/RImw7Z0Ztho

Henry BachmanPost authorI love how these videos are short and still better than a lot of longer videos about the same subject out there.

Salah Din Ahmed Salh RezkPost authorI will solve rubik's cube

GreatestGulagPost authorWouldn't this just be illogical because of how uselessly complex it is.

Gandor 999Post authorIf you let N be each pair of people, then shouldn't N be equal to 1? But contain 2 people?

TurboShamuPost authorwhy does the guy sound like TuxBird?

MSKPost authorI love maths

(To observe and learn from not to apply .. just to clarify)

Stevie1daPost authorN is inefficent, n += 1

Emrah SAKCİPost authorso cool

Saiabhijna PendyalaPost authorSo if there are 4 people in the room,it would be for pairs of people and n=n+4 right??

fazly rabby princePost authorthat was tough to understand tho

Tranquilino PadillaPost authorI don't understand. What is the problem?

Natalie HillPost authorWhat is the song at the end called?

AnantAgarwalPost authorDavid J. Malan – That CS50 guy !!

OSCAR OLVEDAPost authorthe suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuucks

UndoPost authorlet n = number of people in room

Half'n WholePost authorImagine sorting algorithms being personified and Gravity sort just does magic tricks

J-ManPost authorThe algorithm applied today on you tube is on purpose

Deduced by the fact that the person counting has chosen a set set of variables

Approved by and implemented by said powers that be

Logic

hello YouTuberPost authorI thought this video would be harder to understand

Wilfred ChukwuPost authorthank you prof david, you made me learn programming and really getting in depth of what programming is all about

Kushal RijalPost authorWtf???

Michael MonroePost authorWHAT??? tf

DeepakPost authorWell said

Deep MistryPost authorYou rock sir!! CS50 and now this can go long way☺ cheers!!!

Peter StanslowPost authorI enjoyed this alot! Thank you.

Peter StanslowPost authorBinary Code?

JimPost authorthose are the fundamentals of computer programming :3

glancedUpPost authorA set of step by step instructions to do / achieve / solve something.

Aaron EPost authorSo it has nothing to do with Al Gore trying to dance.

Shiv KPost authorExcellent!

Superrv002Post authorI saw this video in 2018 and realized that it was uploaded 5 years ago.

raffaele rimorsoPost authorvery nice lezion

Benniesefun ... testingPost authorWhy not just room.length ?

Girija SankarPost authorI only came here for David j Malan <3

Rolando MotaPost authorAfter 0:42 I was lost

Gursimran AujlaPost authorUr suedo code looks like python code

Prisoner60863Post authorOk so how does this make my phone work? 🤔

LEONARDO RpgnPost authorNegativo

Smoody_x5Post authorit's a good idea, I never think about it.

Nania MekonnenPost authorso helpful…..tnxs

Dave DavisPost authorI'm am so confused after this explanation .

Caleb EckenrodePost authorSub me and I'll sub you back!

Vanshaj VermaPost authorThat's literally introduction to programming

Boi Chuah SangPost authorThe longer I listened the less I understood. Why don't you just tell us the synonym words for this term?

Is it like "process"?

Is it like "steps"?

Is it like "pattern"?

Is it like "system"?

B-Art RainbowPost authorAm I the only one who came here because of the Blue Shirt Kid?

Lakshminarayana Erode VenkatramanPost author'Exam Warriors'..Gold Standards or Bench Marking..Industry Best Practices..Performance Appraisals..Management Development Programme..Contd..Today's Spotlight :MS 07 Information Management and Computers. Love You. Thank you. God Bless You.

What is Algorythm?

mr.1n5an_ePost authorWhen something is very hard to explain or it is too useless to explain, the programmer will make use of the word "Algorithm";

andykishorePost author2:37 Clocks.

i want some buttPost authori dont care about this i just wanna eat some pasta

i want some buttPost authori hate mathhhhh

Youngskilledindia. ComPost authorWhich editing software ???

Tirtham PalPost authorGenius

TheRavMangoPost authorgroningen sent me 😉

Ary de OliveiraPost authornice

Aurobindo GhoshPost authorit means how you calculate what you want to calculate

Kitty Kat CongregationPost authorThe cartoon in the blue sweatshirt lmao

i love it.

Kumar TusharPost authorTed is always best

Ezzahi GamingPost authorNB: The word 'algorithm' has its roots in Latinizing the name of Muhammad ibn Musa al-Khwarizmi in a first step to algorismus. He was a Persian mathematician, astronomer, geographer, and scholar in the House of Wisdom in Baghdad, whose name means 'the native of Khwarazm', a region that was part of Greater Iran and is now in Uzbekistan.

About 825, al-Khwarizmi wrote an Arabic language treatise on the Hindu–Arabic numeral system, which was translated into Latin during the 12th century under the title Algoritmi de numero Indorum. This title means "Algoritmi on the numbers of the Indians", where "Algoritmi" was the translator's Latinization of Al-Khwarizmi's name.[15] Al-Khwarizmi was the most widely read mathematician in Europe in the late Middle Ages, primarily through another of his books, the Algebra.[16] In late medieval Latin, algorismus, English 'algorism', the corruption of his name, simply meant the "decimal number system". In the 15th century, under the influence of the Greek word ἀριθμός 'number' (cf. 'arithmetic'), the Latin word was altered to algorithmus, and the corresponding English term 'algorithm' is first attested in the 17th century; the modern sense was introduced in the 19th century

Moh AmuzaPost authorNow I can Start Counting from Zero

Hajime HinataPost authori can finally count people in my room

Gopal RoyPost authorBeautifully explained…

Indian engineering professor should learn this., morons they just run like idiots while Xplaining coding with C

Ngô Quang ĐạoPost authorVideo good. Great!

Aliens ExistPost authorDam it must feel great to be smart

Ayhan AghalarovPost authorI don't get it. If N=0 , how can N=N+2 meaning how can 0=0+2=2??? wtf. someone take me through this please, the example is too confusing.

HolyWrathPost authorYeeeeees algorithms! Wooooh!

Luis Portillo ReyesPost authorI came here hoping to learn how algorithm works on computers. Maybe next time ☝🏻

Suvashis satapathyPost authorHow about an algorithm to deal with depression? I promised my friend I would help but I dont know how, yet.

Yato YukinePost authorProgramming Basic.

Yona HimePost authorWhat about youtube's algorithm care to write it in a pseudocode too.

Rocky UgsodPost authorI'm currently studying programming but this is still very helpful.

Paco's Small Engine RepairPost authorWtf am I watching?

Mr. T.Post authorIs this guy really teaching people have to count?

Phuc NguyenPost authorjust finished watching cs50 then meet u here again

PoSeiDoN VeNuHPost authorDoes that mean I can use algorithm to study algorithm?

Klent TangaroPost authorHere we go againn!!! Youtube Algorithm has brought us here!!

lotheanPost authorI would use an algorithm to figure out how to get the American president to tell the truth. The algorithm will always fail. Lol

New StudentPost authorI like the sound in the end