...

/

Running Time as a Function of Input Size

Running Time as a Function of Input Size

Learn how the running time of an algorithm is a function of the size of its input.

A toy example

Let’s look at a toy example to help distill our notion of running time. We define a function GetSum that takes an array AA and its length nn as inputs, and returns the sum of all the elements in the array.

Press + to interact
GetSum returns the sum of elements in the input array
GetSum returns the sum of elements in the input array

Viewing the running time as a function

Let’s look at the running time of each instruction in the above algorithm.

Note that each CiC_i in the table below (where i{1,2,3,4}i\in\{1,2,3,4\}) represents some constant specific to the underlying machine. We don’t know its numeric value, but we do know it is some constant number.

Line number Time taken to execute
2 C1C_1
3 C2(n+1)C_2 (n+1)
...