Every step in an algorithm could be assigned a number. Then the running time of the algorithm would simply be the number of steps the algorithm has to complete. This is easily counted for any given input. Let's say we have 8 numbers to sort. We could count how many lines of code get run in order to complete the task. We could do this for any discrete value of N where N is the number of items we are to sort. However we can not do this for all possible values of N nor can we do it for all possible permutations of the unsorted list of values. Some algorithms perform better on different arrays of unsorted numbers. For example, quicksort performs at its worst when the array is almost already sorted.

We need to find a best case, worst case and average case scenario and work out, based on N, roughly how many lines of code will be run. This can be developed into a function of N. So F(N) produces the length of time the algorithm needs to run, on average, given the unsorted list length of N. This process is well out of the scope of this handout and in fact the course.

When we have figured out the formula of N for a given algorithm we can then compare it to other similar algorithms. Bubble sort, on average, produces its results in N 2 time. Quicksort produces its results in N * log 2(N). If we look at different values of N we can quickly see why quicksort is much faster than bubble sort for large values of N.

N

N 2

N * Log 2(N)

10

100

33

100

10000

664

1000

1000000

9966

 

For very small values of N the values for both algorithms are very close. However the larger N becomes the more favourable quicksort becomes.