Search⌘ K
AI Features

Running Time as a Function of Input Size

Explore how to analyze an algorithm's running time in relation to input size, using a practical example. Understand how running times can be expressed as functions involving constants and polynomials, setting the foundation for comparing algorithm efficiency.

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.

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)
...