Posted on ::

I have been using the word algorithm a bit loosely in my conversations lately, so I though I'd clarify a few things.

The modern meaning for algorithm is quite similar to that of recipe, process, method, technique, procedure, routine, rigmarole, except that the word "algorithm" connotes something just a little different. Besides merely being a finite set of rules that gives a sequence of operations for solving a specific type of problem, an algorithm has five important features — The Art of Computer Programming, Volume 1, p. 4.

In summary, those 5 features are:

  • Finiteness. It must always terminate after a finite number of steps. It can't run forever.
  • Definiteness. Each step must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case.
  • Input. It has zero or more inputs. Values may be given to the algorithm before it begins to use as an input.
  • Output. It has at least one output, and said output or outputs will have a relation to the input.

These definitions are valid across all programming languages; they are fundamental in nature.

In addition to those 5 features, and algorithm will likely make use of intermediary calculated values, or variables. And a useful way to design and test an algorithm, is by running through an example. For instance, let's say I want to make an algorithm to calculate the factorial of a number, I may write something like:

name: factorial
inputs: n
outputs: result

n < 0 -> result is not defined
n = 0 -> result = 1
n > 0 -> result = factorial of (n-1)

description: I will multiply 1, 2, 3, 4... up to n and return it as a result.

define auxiliary variable: i

1: if n is < 0 return an error, as this is undefined
2: result = 1, i = 1
3: if n equals 0 return result and end execution here, otherwise continue
4: if i > n return result, otherwise continue
5: multiply result by i, store it in result
6: increment i by 1
7: go to line 4

test:

factorial of 5

1:  5 is not negative, so we continue
2:  result = 1, i = 1
3:  5 is greater than 0, so we continue
4:  i is 1 and 1 < 5, so we continue
5:  1 * 1 = 1, result = 1
6:  i = 2
7:  i is 2 and 2 < 5, so we continue
8:  1 * 2 = 2, result = 2
9:  i = 3
10: i is 3 and 3 < 5, so we continue
11: 2 * 3 = 6, result = 6
12: i = 4
13: i is 4 and 4 < 5, so we continue
14: 6 * 4 = 24, result = 24
15: i = 5
16: i is 5 and 5 = 5, so we continue
17: 24 * 5 = 120, result = 120
18: i = 6
19: i is 6, 6 > 5 so we stop execution and return result

result = 120

It might seem tedious at first, but the more you practice the quicker you'll get, picking up patterns along the way, going back and improving previous algorithms you build as well!

I see writing things down as a way to slow down the thinking process and being able to focus on the details, but everyone is different. I read somewhere that Nikola Tesla was known to devise entire machine designs in his head, and then build them directly; which is definitely not how it works for me.

Give a try!