Algorithms

An algorithm is a series of steps that you follow to do something. An instruction manual or a recipe are good examples of algorithms. We translate algorithms into code when we are programming.

There are a series of very famous algorithms in computer science. Most of the Turing Award winners won for algorithms that they wrote. Tony Hoare won for his QuickSort algorithm, Dijkstra for his networking algorithms, Hamming for his Hamming Code algorithms and many more.

These are the key aspects of a great algorithm:

Big-Oh Notation

Big-Oh is the way that we say how fast a program runs. We don’t like to use seconds or milli-seconds because different computers run different speeds. Instead we measure in terms of the number of elements in the array or n.

A sequence of times from fastest to slowest:

  1. constant time (1)
  2. log n
  3. n
  4. n log n
  5. n2
  6.   n3

Here are some basic lengths of things in terms of big-oh:

Piece of Code

Time

Assignment Statement

O(1)

Single For Loop Through an Array

O(n)

Nested For Loop Through an Array

O(n2)

If

O(1)

Why study sorting?

  1. Sorting is a very common function. Every computer scientist knows a bunch of different ways of sorting.
  2. Sorting allows us to practice using arrays and for loops.
  3. The different ways of sorting are called algorithms. All advanced computer science is based on algorithms.

Types of Sorting

Bubble Sort (stops early when sorted)

Shaker Sort

Selection Sort

Insertion Sort

Bin Sort

Examples:

Bubble

4321
3421
3241
3214
2314
2134
1234

Shaker

4321
3421
3241
3214
3124
1324
1234

Selection

4321
1324
1234

Insertion

4321
3421
2341
1234

Bin

1234422113

1’s = 3
2’s = 3
3’s = 2
4’s = 2

1112223344