What is an algorithm


See "List of Algorithms." Wikipedia for more.


Example - Adding a set of numbers

Algorithm

Step 1: Create an array of numbers
Step 2: Create a place to store the sum
Step 3: Take next number from the array
Step 4: Add it to the sum variable
Repeat 3 and 4 until none remain
Step 5: Print the sum

Code

number_list = [1,2,3,4,5]
sum = 0
for a_number in number_list:
    sum = sum + a_number
print (sum)

Performance of Algorithms

Algorithm A

START
K! = 1 x ... x K
PRINT K!
END
$$\begin{align} & K! = 1\times2... \times K\\ &e.g.\\ &1! = 1 \\ &2! = 1 \times 2 \\ &3! = 1 \times 2 \times 3 \\ &4! = 1 \times 2 \times 3 \times 4\\ &...\end{align}$$ Starts from 1 every time

Algorithm B

START
K! = (K-1)! x K
PRINT K!
END
$$\begin{align} &K! = (K-1)! \times K\\ &e.g.\\ &1! = 1 \\ &2! = 1! \times 2\\ &3! = 2! \times 3\\ &4! = 3! \times 4\\ &...\end{align}$$ Starts with the previous result $(K-1)!$

NUMBER OF STEPS0100200300400SIZE OF INPUT(N)510152025

Order of Growth - Big O - $\mathcal{O}_n$

Performance of algorithms is characterized by their Order of Growth (or Big-O or \(\mathcal{O}_n\)), which indicates how completion time (or memory used) grows with increasing size of inputs.

NUMBER OF STEPSSIZE OF INPUT

Read more about Algorithms at Sedgewick, Robert and Wayne, Kevin. Algorithms, 4th Edition.