1.4Common Running Times: Constant to Cubic
Covers IGNOU MCS-211 §1.4
Many algorithms can solve the same problem, so we need a fair way to compare them. This page explains how running time is measured and introduces the most common “polynomial” running times, with the loop shapes that produce them.
Measuring running time
Measuring seconds on a stopwatch is unfair: the result depends on the computer, the compiler and the other programs running. Instead we count the number of basic operations as a function of the input size.
We usually care about how grows as becomes large, and we ignore constant factors. That is why we write (read “big-O of ”) rather than . Big-O is defined formally in Section “Analysis of Algorithms” and in Unit 2; for now read as “grows at most like ”.
The classes
| Class | Name | Typical loop shape | Example |
|---|---|---|---|
| Constant | No loop over the input | Read a[i]; push onto a stack | |
| Logarithmic | Problem size halves each step | Binary search | |
| Linear | One pass over the input | Find the minimum of an array | |
| Linearithmic | Split in halves, do linear work per level | Merge sort, heap sort | |
| Quadratic | Two nested loops over the input | Bubble, selection, insertion sort (worst case) | |
| Cubic | Three nested loops | Naive matrix multiplication; the disjoint-sets example below |
Constant time,
The number of steps does not depend on . Swapping two variables or reading the first element of an array takes the same time whether the array has 10 elements or 10 million.
Logarithmic time,
If each step halves the remaining problem, the number of steps is about . Binary search in a sorted array is the standard example.
Linear time,
The algorithm does a constant amount of work for each input element, usually in one pass. Finding the minimum:
minimum ← a[1]
for i ← 2 to n do
if a[i] < minimum then
minimum ← a[i]
end if
end forThis makes exactly comparisons, so .
Linearithmic time,
Divide-and-conquer sorting algorithms such as merge sort split the array into halves (about levels of splitting) and do work to merge on each level, giving in total. It grows only slightly faster than linear time and is the best possible for sorting by comparisons.
Quadratic time,
Two nested loops, each running up to times, perform up to inner steps. Simple sorting algorithms (bubble, selection, insertion) are quadratic in the worst case. Quadratic algorithms are fine for small inputs but become slow for large ones.
Cubic time,
Three nested loops give up to steps. The study material’s example: given sets , each with elements, decide whether some pair of the sets is disjoint (has no common element).
for each set S_i do
for each other set S_j do
common ← false
for each element x of S_i do
if x belongs to S_j then // O(1) test, see below
common ← true
end if
end for
if common = false then
print "S_i and S_j are disjoint"
end if
end for
end forThere are choices of , at most choices of and elements , so at most membership tests. The analysis assumes each test “is in ?” takes constant time, for example by storing each set as a true/false table indexed by element. If the test instead scanned element by element, it would cost and the total would become .
#include <stdio.h>
#include <stdbool.h>
#define N 4 /* number of sets, and size of each set */
#define U 16 /* elements are numbers 0..U-1 */
/* member[i][x] is true when x is in set S_i.
This table makes the test "is x in S_j?" take O(1) time. */
static bool member[N][U];
int main(void)
{
int sets[N][N] = {
{1, 2, 3, 4},
{4, 5, 6, 7},
{8, 9, 10, 11},
{3, 9, 12, 13}
};
long checks = 0;
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
member[i][sets[i][k]] = true;
for (int i = 0; i < N; i++) { /* each set S_i */
for (int j = i + 1; j < N; j++) { /* each other set S_j */
bool common = false;
for (int k = 0; k < N; k++) { /* each element x of S_i */
checks++;
if (member[j][sets[i][k]]) { /* O(1) membership test */
common = true;
break;
}
}
if (!common)
printf("S%d and S%d are disjoint\n", i + 1, j + 1);
}
}
printf("membership checks: %ld (at most n^3 = %d)\n", checks, N * N * N);
return 0;
}S1 and S3 are disjoint
S2 and S3 are disjoint
S2 and S4 are disjoint
membership checks: 21 (at most n^3 = 64)The program checks each unordered pair once (j > i) and stops early when it finds a common element, so it uses fewer than checks; is the upper bound.
Counting steps for each loop shape
#include <stdio.h>
/* Counts how many times the innermost statement runs
for loop shapes that give common running times. */
int main(void)
{
int n = 16;
long constant = 0, logarithmic = 0, linear = 0, quadratic = 0, cubic = 0;
constant++; /* O(1): one step, any n */
for (int size = n; size > 1; size /= 2) /* O(log n): halve each time */
logarithmic++;
for (int i = 0; i < n; i++) /* O(n): one pass */
linear++;
for (int i = 0; i < n; i++) /* O(n^2): two nested loops */
for (int j = 0; j < n; j++)
quadratic++;
for (int i = 0; i < n; i++) /* O(n^3): three nested loops */
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
cubic++;
printf("n = %d\n", n);
printf("constant : %ld\n", constant);
printf("logarithmic : %ld\n", logarithmic);
printf("linear : %ld\n", linear);
printf("quadratic : %ld\n", quadratic);
printf("cubic : %ld\n", cubic);
return 0;
}n = 16
constant : 1
logarithmic : 4
linear : 16
quadratic : 256
cubic : 4096Key takeaways
- Running time counts basic operations as a function of input size .
- Constant factors are ignored inside big-O: write and , not and .
- Halving gives ; one pass gives ; nested loops multiply.
- The disjoint-sets bound assumes membership tests.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Find the number of times x ← x + 1 runs in: for i ← 1 to n do for j ← 1 to n do for k ← 1 to n do x ← x + 1. Give its class.
Show worked answer
The three loops are independent and each runs n times, so the statement runs n × n × n = n³ times. The running time is O(n³) (cubic). For n = 10 that is 1000 executions.