# Beginning Programming-Writing an Algorithm: What's an algorithm?

**URL:** <https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834>\
**Category:** Factual Questions\
**Created:** [January 24, 2009, 9:53pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834 "2009-01-24T21:53:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![handsomeharry](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@handsomeharry](https://boards.straightdope.com/u/handsomeharry)\
**Post date:** [January 24, 2009, 9:53pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/1 "2009-01-24T21:53:15Z")

</div>

I have enrolled in a Beginning Programming class at the local CC. It has no prerequisites listed other than English comprehension, etc…  
I glanced at the first assignment and it says something to the effect of “Write an algorithm” …something about a 52 card deck.  
OK, if there are no Prereqs of note, what am I doing writing an algorithm? Isn’t that something College Algebra level? I graduated with a BA in History, and haven’t had to do College Algebra, I just got to Intermediate Algebra. Can I learn it in two nights? If so, where do I learn this?  
This has me mortified!  
Can anybody help?  
Thanks,  
hh

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [January 24, 2009, 9:53pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/2 "2009-01-24T21:53:48Z")

</div>

“algorithm” is just a fancy word for “program”. [The resemblance to “algebra” is purely etymological (the ancient mathematician Al-Khwārizmī wrote a book whose title included “al-Jabr”, providing the origin of both words), but that’s the only connection.]

College Algebra probably has nothing to do with your task. Just ignore the word “algorithm”, finish reading what you’ve been assigned to do, and then write a program that does it.

---

<div class="post-metadata">

**Author:** ![handsomeharry](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@handsomeharry](https://boards.straightdope.com/u/handsomeharry)\
**Post date:** [January 24, 2009, 9:55pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/3 "2009-01-24T21:55:44Z")

</div>

PS, I forgot to add that I haven’t even opened the book, because I won’t get it until Monday, or gone to class-it’s online.  
Thanks,  
hh

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 24, 2009, 9:56pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/4 "2009-01-24T21:56:35Z")

</div>

An algorithm is just a series of steps for completing a task. For example, consider the task: put a deck of cards into a random order.

Here’s an algorithm that works:

1. Divide the deck into two equal piles
2. Shuffle the two piles together
3. Repeat several times.  
Bam, you’ve got an algorithm.

---

<div class="post-metadata">

**Author:** ![Musicat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/musicat/32/20189_2.png) [@Musicat](https://boards.straightdope.com/u/Musicat)\
**Post date:** [January 24, 2009, 9:57pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/5 "2009-01-24T21:57:06Z")

</div>

Think of it as “a procedure to accomplish a task.” Or a program.

They may want you just to outline the steps to take to solve a problem in pseudo-code, not necessarily in an executable language.

What’s the actual task about the 52-card deck?

---

<div class="post-metadata">

**Author:** ![handsomeharry](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@handsomeharry](https://boards.straightdope.com/u/handsomeharry)\
**Post date:** [January 24, 2009, 9:57pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/6 "2009-01-24T21:57:22Z")

</div>

> [@Indistinguishable](#):
>
> “algorithm” is just a fancy word for “program”. The link to “algebra” is purely etymological; both words come from Arabic (Al-Khwārizmī wrote a math book whose title included “al-Jabr”), but that’s the only connection.
> 
> College Algebra probably has nothing to do with your task. Just ignore the word “algorithm”, finish reading what you’ve been assigned to do, and then write a program that does it.

Ahhh…a load off of my mind.  
You’ve helped very quickly.  
Thanks,  
grateful hh

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [January 24, 2009, 10:05pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/7 "2009-01-24T22:05:26Z")

</div>

An algorithm is a set of instructions to accomplish a task.

You generally implement algorithms in a particular programming language to do what you want to do. Consider a simple math problem, and describe the specific steps you’d take to solve it.

For example, given a list of numbers how do you find the biggest one?

1. start by assuming that the first number is the largest.
2. Go through each element in the list and compare it to the largest value you’ve found.  
2a. If it’s larger, it becomes the new largest value found.  
2b. If not, continue.
3. When you get to the end of the list, you’re done.

It’s a simple algorithm, but being able to break down the process into very specific steps is important for programming.

I don’t know what your algorithm needs to be for the deck of cards, but realize that a deck of cards is really just a list of numbers (and suits, but you can generalize those as numbers, too, depending on what you want to do with them).

---

<div class="post-metadata">

**Author:** ![handsomeharry](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@handsomeharry](https://boards.straightdope.com/u/handsomeharry)\
**Post date:** [January 24, 2009, 10:10pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/8 "2009-01-24T22:10:27Z")

</div>

> [@Musicat](#):
>
> Think of it as “a procedure to accomplish a task.” Or a program.
> 
> They may want you just to outline the steps to take to solve a problem in pseudo-code, not necessarily in an executable language.
> 
> What’s the actual task about the 52-card deck?

Well, I can’t remember: I am away from the computer that I use, and I don’t have my password here. I was so shocked by the A word that I didn’t have a heart to actually read the rest of it, I just sort of glanced at the length of it, and at the next question, and THAT had algorithm in it, and I closed the window in panic, and headed out to go to work! (The word ‘idiot’ comes to mind here…I wonder why?:dubious:)  
If I can get to it anytime soon, I’ll get back.  
Thanks a lot!  
hh

---

<div class="post-metadata">

**Author:** ![KlondikeGeoff](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KlondikeGeoff](https://boards.straightdope.com/u/KlondikeGeoff)\
**Post date:** [January 24, 2009, 10:34pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/9 "2009-01-24T22:34:18Z")

</div>

> [@handsomeharry](#):
>
> I was so shocked by the A word that I didn’t have a heart to actually read the rest of it

It all is not as scary as you might think. You must remember that computers are really, really simple-minded. They only seem smart because they can run a program so fast.

To tell a computer to do a task (write a program), you must remember that. You can tell a person, “Pick up a pencil and write your name,” and almost anybody can understand that instruction.

To tell a computer to do it, you have to do something such as:

001 See the pencil on the table  
002 reach out your arm  
003 when on top of the pencil, lower your arm over the pencil  
004 stop just above the pencil  
005 open your fingers  
006 lower your fingers on each side of the pencil  
007 close your fingers on the pencil  
008 lift your arm with the pencil in your fingers  
etc, etc.

The thing is, once the computer absorbs those commands, it will do the instructed task in in a fraction of a second or so.

Therefore, don’t let the thing intimidate you. You are much smarter than the computer. 😃

---

<div class="post-metadata">

**Author:** ![Voyager](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/voyager/32/133_2.png) [@Voyager](https://boards.straightdope.com/u/Voyager)\
**Post date:** [January 25, 2009, 1:51am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/10 "2009-01-25T01:51:52Z")

</div>

> [@handsomeharry](#):
>
> Well, I can’t remember: I am away from the computer that I use, and I don’t have my password here. I was so shocked by the A word that I didn’t have a heart to actually read the rest of it, I just sort of glanced at the length of it, and at the next question, and THAT had algorithm in it, and I closed the window in panic, and headed out to go to work! (The word ‘idiot’ comes to mind here…I wonder why?:dubious:)  
> If I can get to it anytime soon, I’ll get back.  
> Thanks a lot!  
> hh

Maybe this won’t help, but if you ever cooked something following a recipe, you also dealt with an algorithm. If you ever built something using Legos and following the directions, that was an algorithm also. Don’t sweat it.

---

<div class="post-metadata">

**Author:** ![sailor](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@sailor](https://boards.straightdope.com/u/sailor)\
**Post date:** [January 25, 2009, 1:56am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/11 "2009-01-25T01:56:28Z")

</div>

The old Babylonian algorithm for obtaining the square root of N with precission p:

1- S = N/2  
2- a = N/S  
3- S = (S+a)/2  
4- a = N/S  
5- IF ABS(S-a) \> p GOTO 3  
6- PRINT “Square Root of” N “is” S

---

<div class="post-metadata">

**Author:** ![engineer\_comp\_geek](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/engineer_comp_geek/32/504_2.png) [@engineer\_comp\_geek](https://boards.straightdope.com/u/engineer_comp_geek)\
**Post date:** [January 25, 2009, 3:53am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/12 "2009-01-25T03:53:19Z")

</div>

> [@Indistinguishable](#):
>
> “algorithm” is just a fancy word for “program”.

I would disagree with this. The algorithm is the steps used to solve the problem. The program is the implementation of those steps.

For example, one of the simplest algorithms that is taught to beginning programmers is the “bubble sort”. For a bubble sort, the “algorithm” is that you repeatedly go through the list, constantly checking each item in the list with the next item in the list. If they are in the correct sort order, you leave them alone. If not, you swap the two of them. You have to go through the list as many times as you have elements. In other words, if you have 10 items in your list, you have to go through the list 10 times. Then everything is sorted. If you have 100 items, you have to go through the list 100 times.

You can implement this algorithm in numerous different ways. A C program that does a bubble sort could look like this:

```auto

for (i=0; i<n-1; i++) {
  for (j=0; j<n-1-i; j++)
    if (a[j+1] < a[j]) { /* compare the two neighbors */
      tmp = a[j]; /* swap a[j] and a[j+1] */
      a[j] = a[j+1];
      a[j+1] = tmp;
  }
}

```

A BASIC bubble sort would look like this:

```auto

2000 REM Bubble sort the list A of length N
2010 FOR I = 1 TO N - 1
2020 FOR J = 1 TO N - I
2030 IF A[J] <= A[J+1] THEN GOTO 2070
2040 LET X = A[J]
2050 LET A[J] = A[J+1]
2060 LET A[J+1] = X 
2070 NEXT J
2080 NEXT I
2090 RETURN

```

Even though the program is different in each case, the algorithm is the same for both programs.

You could do a bubble sort with a deck of cards in your hands. The algorithm would be the same, but the “program” would be significantly different than a computer program. The algorithm is the step by step method that you use to solve the problem. The program is the implementation of the algorithm.

Get it?

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [January 25, 2009, 3:57am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/13 "2009-01-25T03:57:57Z")

</div>

> [@engineer\_comp\_geek](#):
>
> I would disagree with this. The algorithm is the steps used to solve the problem. The program is the implementation of those steps.

For the first assignment of an introductory programming class, it’s not worth making this distinction.

---

<div class="post-metadata">

**Author:** ![FrustratedIdiot](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@FrustratedIdiot](https://boards.straightdope.com/u/FrustratedIdiot)\
**Post date:** [January 25, 2009, 5:26am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/14 "2009-01-25T05:26:05Z")

</div>

> [@engineer\_comp\_geek](#):
>
> A C program that does a bubble sort could look like this:
> 
> ```auto
> 
> for (i=0; i<n-1; i++) {
> for (j=0; j<n-1-i; j++)
> if (a[j+1] < a[j]) { /* compare the two neighbors */
> tmp = a[j]; /* swap a[j] and a[j+1] */
> a[j] = a[j+1];
> a[j+1] = tmp;
> }
> }
> 
> ```
> 
> Get it?

Giving the guy actual code may confuse him. These algorithm examples are simple only to those who are familiar with code.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 25, 2009, 5:28am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/15 "2009-01-25T05:28:31Z")

</div>

> [@FrustratedIdiot](#):
>
> Giving the guy actual code is only going to confuse him. These algorithm examples are simple only to those who are familiar with code.

And who the hell starts a BASIC program with line number 2000? I mean, come on!

---

<div class="post-metadata">

**Author:** ![rowrrbazzle](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/rowrrbazzle/32/414_2.png) [@rowrrbazzle](https://boards.straightdope.com/u/rowrrbazzle)\
**Post date:** [January 25, 2009, 5:34am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/16 "2009-01-25T05:34:50Z")

</div>

> [@KlondikeGeoff](#):
>
> It all is not as scary as you might think. You must remember that computers are really, really simple-minded. …
> 
> Therefore, don’t let the thing intimidate you. You are much smarter than the computer. 😃

Well, from my perspective as a business programmer, both of those things should scare the OP a bit. Computers _are_ simple-minded. They do what you tell them to do. Exactly. Think that over. 😃

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [January 25, 2009, 5:48am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/17 "2009-01-25T05:48:21Z")

</div>

> [@engineer\_comp\_geek](#):
>
> I would disagree with this. The algorithm is the steps used to solve the problem. The program is the implementation of those steps.

I don’t know that that is a standard terminological distinction, but it’s true that this assignment may just be asking for an English or pseudocode description of a program, and not actual source code in some computer language, as pointed out before. But whatever it asks for, I’m sure it’s something the OP will be capable of handling; they just needed to know not to be scared off by the word “algorithm”.

---

<div class="post-metadata">

**Author:** ![633squadron](https://avatars.discourse-cdn.com/v4/letter/6/ee59a6/32.png) [@633squadron](https://boards.straightdope.com/u/633squadron)\
**Post date:** [January 25, 2009, 5:56am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/18 "2009-01-25T05:56:53Z")

</div>

> [@handsomeharry](#):
>
> I have enrolled in a Beginning Programming class at the local CC. It has no prerequisites listed other than English comprehension, etc…  
> I glanced at the first assignment and it says something to the effect of “Write an algorithm” …something about a 52 card deck.

Don’t Panic!

Programming is not particularly about math. It’s more about orderly thinking. I bet you that you already know a bunch of algorithms, though you don’t know them by that name!

For example, you have to have to find the result of the multiplication

11x13 = ?

I dunno about _you_, but when _I_ was learning multiplication, we did it with pencil and paper with this algorithm:

Put the numbers one over the other

```auto

     11
     13
     ==

```

Multiply the top number by the rightmost number on the bottom:

3 x 11 and write down the result so that the rightmost digit of the result is under the bottom number you just used.

```auto

     11
     13
     ==
     33

```

Now multiply the top number by the next number to the left on the bottom, and write down the result so that the rightmost digit of the result is under the bottom number you just used.

```auto

    11
    13
    ==
    33
   11

```

When you run out of numbers on the bottom, add up the results you put below by columns, just as if you were adding a list of numbers. Blanks become zeros:

```auto

    11
    13
    ==
    33
   110
  =====
   143

```

So 11x13 is 143, using an algorithm for multiplying two numbers. Of course there are others, but this is a simple one.

It’s not important _why_ it works. What’s important is that it’s a good way to multiple together two numbers of a reasonable size (say, out to 3 digits) for any number!

In programming, you’ll learn some other algorithms for solving problems.

Computers are really really good at repeating things lots of times real fast, doing math calculations, and testing things. Mostly what you learn in studying computers is how to chop apart problems into bits that involve repetition, calculation, and testing; that is, how to create and use algorithms.

There’s data as well. That’s fun too, but it’s getting late. Hope this helps.  
😃

---

<div class="post-metadata">

**Author:** ![633squadron](https://avatars.discourse-cdn.com/v4/letter/6/ee59a6/32.png) [@633squadron](https://boards.straightdope.com/u/633squadron)\
**Post date:** [January 25, 2009, 5:59am UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/19 "2009-01-25T05:59:09Z")

</div>

> [@engineer\_comp\_geek](#):
>
> For example, one of the simplest algorithms that is taught to beginning programmers is the “bubble sort”.

I think the OP already fell over in a dead faint.:eek:

---

<div class="post-metadata">

**Author:** ![ftg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ftg/32/2801_2.png) [@ftg](https://boards.straightdope.com/u/ftg)\
**Post date:** [January 25, 2009, 2:23pm UTC](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834/20 "2009-01-25T14:23:48Z")

</div>

> [@ultrafilter](#):
>
> For the first assignment of an introductory programming class, it’s not worth making this distinction.

Strongly disagree here. It is absolutely crucial that beginners learn the distinction ASAP. Computer Science is all about abstraction. And the difference between a program (concrete) and an algorithm (abstract) is lesson 1.

For noobs and such I make the following point: a program is what you tell a computer, an algorithm is what you tell a human.

[Next page](https://boards.straightdope.com/t/beginning-programming-writing-an-algorithm-whats-an-algorithm/482834.md?page=2)
