A data structure is used through algorithms. To compare algorithms, we measure how their running time and memory grow with the size of the input.

Algorithms

An algorithm is a finite sequence of well-defined steps that solves a problem. A good algorithm has input, output, definiteness (each step is clear), finiteness (it stops after a finite number of steps) and effectiveness (each step can be carried out). Algorithms are written in pseudocode or as flowcharts before coding.

Definition: Time complexity. A measure of the amount of time an algorithm takes as a function of the size of its input, usually expressed by counting the basic operations performed.

Complexity of Algorithms

The time complexity of an algorithm is the number of basic steps it performs as a function of the input size n. The space complexity is the amount of memory it needs. We usually study the worst case, the maximum steps for any input of size n, and sometimes the average case and best case. Because exact counts depend on the machine, we use asymptotic notation, which describes the growth rate for large n. Big-O, O(f(n)), gives an upper bound: the algorithm takes at most a constant times f(n) steps. Omega, Ω, gives a lower bound, and Theta, Θ, a tight bound. Common classes, from fastest to slowest, are O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), O(n²) quadratic and O(2ⁿ) exponential. For n = 1,000,000, an O(n) algorithm takes a million steps, while an O(n²) one takes a million million.

Time-Space Tradeoff

Often we can make an algorithm faster by using more memory, or save memory by doing more work. This is the time-space tradeoff. Storing precomputed results in a table, called a lookup table, makes later queries fast but uses memory. Keeping data sorted makes searching faster but insertion slower. Compressing data saves space but needs time to decompress. The best choice depends on which resource is scarcer for the problem.

Table 1.2: Growth of common complexities

Complexity n = 10 n = 100 n = 1000
O(log n) 3 7 10
O(n) 10 100 1,000
O(n log n) 33 664 9,966
O(n²) 100 10,000 1,000,000

Example 1.2: Counting steps

Problem: Find the time complexity of (a) a loop that adds n numbers, (b) two nested loops each running n times, (c) a loop that halves n until it reaches 1.

Solution:

  1. One pass over n numbers takes about n steps: O(n).
  2. n passes of n steps each take n² steps: O(n²).
  3. Halving n reaches 1 after about log₂ n steps: O(log n).

Result: (a) O(n), (b) O(n²), (c) O(log n).

#include <stdio.h>
int main(void) {
    int n = 1000, count = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            count++;
    printf("nested loops: %d steps\n", count);
    count = 0;
    for (int k = n; k > 1; k /= 2) count++;
    printf("halving loop: %d steps\n", count);
    return 0;
}

Output

nested loops: 1000000 steps
halving loop: 9 steps

Common mistakes

  • Comparing algorithms by timing them once on one small input.
  • Keeping constants and lower terms in Big-O. O(3n² + 5n) is O(n²).
  • Ignoring memory when choosing a faster algorithm.

Key takeaways

  • An algorithm is finite, definite and effective, with input and output.
  • Time complexity counts steps, space complexity counts memory, as functions of n.
  • Big-O gives an upper bound on growth.
  • More memory can often buy more speed: the time-space tradeoff.

Practice questions

  1. What is Big-O notation?
  2. What is the complexity of two nested loops each of n steps?
  3. What is the time-space tradeoff?

Answers to the practice questions

  1. A notation that gives an upper bound on the growth rate of an algorithm's cost for large input.
  2. O(n²).
  3. Using more memory to save time, or more time to save memory.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I