Reading history

Order of Growth: How Algorithms Scale

10 min read 4 views

Understand order of growth: why constants are dropped, how to rank growth rates from constant to factorial, and how to compare two algorithms quickly.

Order of growth describes how fast an algorithm’s work rises as the input gets larger. It is the idea that lets you compare two algorithms without timing them.


Why Growth Matters More Than Speed

What is Order of Growth?

Order of growth describes the rate at which an algorithm’s time or space requirement increases relative to input size. It focuses on the dominant term that determines long-term behavior, ignoring constants and lower-order terms.

Formal definition:
A function f(n) grows faster than g(n) if:

  • g(n)/f(n) → 0 as n → ∞
  • Or f(n)/g(n) → ∞ as n → ∞

Why Order of Growth Matters

  • Algorithm comparison: Determine which algorithm scales better.
  • Performance prediction: Estimate behavior for large inputs.
  • Optimization: Focus on the dominant factor.
  • Resource planning: Choose appropriate hardware/resources.
  • Theoretical understanding: Classify algorithms by efficiency.

Standard Order of Growth Hierarchy

From slowest to fastest growing:

c < log log n < log n < n^(1/3) < n^(1/2) < n < n log n < n² < n² log n < n³ < n⁴ < 2ⁿ < nⁿ

Where c is a constant.

Common Growth Rates

OrderNameExample
O(1)ConstantArray access
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort
O(n³)CubicMatrix multiplication
O(2ⁿ)ExponentialRecursive Fibonacci
O(n!)FactorialPermutations

Ranking Common Growth Rates

Finding Order of Growth

When n ≥ 0, f(n) ≥ 0, and g(n) ≥ 0, follow these steps:

  1. Ignore lower-order terms (terms with smaller growth rates).
  2. Ignore constants (coefficients).

Example 1:

f(n) = 4n² + 3n + 100
Step 1: Ignore lower-order terms → 4n²
Step 2: Ignore constants → n²
Order of growth: n²

Example 2:

f(n) = 100n log n + 3n + 100 log n + 2
Step 1: Ignore lower-order terms → 100n log n
Step 2: Ignore constants → n log n
Order of growth: n log n

Comparing Two Order of Growths

To compare algorithms, compare their growth rates:

#include <stdio.h>

/* O(n) algorithm */
int linearSearch(int arr[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == target) return i;
    }
    return -1;
}

/* O(log n) algorithm */
int binarySearch(int arr[], int n, int target) {
    int left = 0, right = n - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

int main(void) {
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Linear search: O(n)\n");
    printf("Binary search: O(log n)\n");
    printf("For large n, binary search is much faster.\n");

    return 0;
}

Growth Rate Examples in C

O(1) — Constant

#include <stdio.h>

int getElement(int arr[], int index) {
    return arr[index];   // O(1): Direct access
}

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    printf("Element: %d\n", getElement(arr, 2));
    return 0;
}

O(log n) — Logarithmic

#include <stdio.h>

/* O(log n): Halves search space each iteration */
int binarySearch(int arr[], int n, int target) {
    int left = 0, right = n - 1;
    int steps = 0;

    while (left <= right) {
        steps++;
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            printf("Found in %d steps\n", steps);
            return mid;
        }
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

int main(void) {
    int arr[100];
    for (int i = 0; i < 100; i++) arr[i] = i * 2;

    binarySearch(arr, 100, 50);
    return 0;
}

O(n) — Linear

#include <stdio.h>

/* O(n): Single loop */
int sumArray(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum;
}

int main(void) {
    int arr[] = {1, 2, 3, 4, 5};
    printf("Sum: %d\n", sumArray(arr, 5));
    return 0;
}

O(n log n) — Linearithmic

#include <stdio.h>
#include <stdlib.h>

/* Comparison function for qsort */
int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

int main(void) {
    int arr[] = {5, 2, 8, 1, 9, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    /* qsort is O(n log n) */
    qsort(arr, n, sizeof(int), compare);

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

O(n²) — Quadratic

#include <stdio.h>

/* O(n²): Nested loops */
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main(void) {
    int arr[] = {5, 2, 8, 1, 9};
    int n = sizeof(arr) / sizeof(arr[0]);

    bubbleSort(arr, n);

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

Comparing Algorithms by Growth in C

This program demonstrates different orders of growth with measurable performance differences.

Program: Order of Growth Comparator

/**
 * order_of_growth.c
 * Demonstrates different orders of growth with performance comparison.
 * Shows how growth rate affects execution time as input size increases.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <math.h>

#define MAX_LINE_LEN 100

/* Function prototypes */
void demonstrateConstantGrowth(void);
void demonstrateLogarithmicGrowth(void);
void demonstrateLinearGrowth(void);
void demonstrateLinearithmicGrowth(void);
void demonstrateQuadraticGrowth(void);
void demonstrateExponentialGrowth(void);
void demonstrateGrowthComparison(void);
void printGrowthTable(int maxN);
long long measureOperation(long long n, int type);

int main(void) {
    int choice;
    char buffer[MAX_LINE_LEN];

    printf("=== Order of Growth Demo ===\n");

    while (1) {
        printf("\n--- Menu ---\n");
        printf("1. O(1) - Constant\n");
        printf("2. O(log n) - Logarithmic\n");
        printf("3. O(n) - Linear\n");
        printf("4. O(n log n) - Linearithmic\n");
        printf("5. O(n^2) - Quadratic\n");
        printf("6. O(2^n) - Exponential\n");
        printf("7. Growth Comparison Table\n");
        printf("8. Exit\n");
        printf("Enter choice: ");
        fflush(stdout);

        if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
            fprintf(stderr, "Error reading choice.\n");
            return EXIT_FAILURE;
        }

        choice = atoi(buffer);

        switch (choice) {
            case 1:
                demonstrateConstantGrowth();
                break;
            case 2:
                demonstrateLogarithmicGrowth();
                break;
            case 3:
                demonstrateLinearGrowth();
                break;
            case 4:
                demonstrateLinearithmicGrowth();
                break;
            case 5:
                demonstrateQuadraticGrowth();
                break;
            case 6:
                demonstrateExponentialGrowth();
                break;
            case 7:
                demonstrateGrowthComparison();
                break;
            case 8:
                printf("Exiting.\n");
                return EXIT_SUCCESS;
            default:
                printf("Invalid choice. Please try again.\n");
                break;
        }
    }

    return EXIT_SUCCESS;
}

/* O(1): Constant growth */
void demonstrateConstantGrowth(void) {
    printf("\n=== O(1) Constant Growth ===\n");

    /* Array access is O(1) regardless of array size */
    int smallArr[10] = {0};
    int largeArr[1000000] = {0};

    printf("Accessing small array element: O(1)\n");
    printf("Accessing large array element: O(1)\n");
    printf("Both take same time regardless of size.\n");
}

/* O(log n): Logarithmic growth */
void demonstrateLogarithmicGrowth(void) {
    printf("\n=== O(log n) Logarithmic Growth ===\n");

    for (int n = 10; n <= 1000000; n *= 10) {
        int steps = 0;
        int temp = n;

        /* Count steps for binary search */
        while (temp > 1) {
            temp /= 2;
            steps++;
        }

        printf("For n = %7d, log2(n) = %2d steps\n", n, steps);
    }
}

/* O(n): Linear growth */
void demonstrateLinearGrowth(void) {
    printf("\n=== O(n) Linear Growth ===\n");

    for (int n = 1000; n <= 1000000; n *= 10) {
        clock_t start = clock();

        volatile int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += i;
        }

        clock_t end = clock();
        double elapsed = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;

        printf("n = %7d: %.3f ms (linear)\n", n, elapsed);
    }
}

/* O(n log n): Linearithmic growth */
void demonstrateLinearithmicGrowth(void) {
    printf("\n=== O(n log n) Linearithmic Growth ===\n");

    for (int n = 1000; n <= 100000; n *= 10) {
        clock_t start = clock();

        /* Simulate n log n operations */
        volatile int sum = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j *= 2) {
                sum += i * j;
            }
        }

        clock_t end = clock();
        double elapsed = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;

        printf("n = %7d: %.3f ms (n log n)\n", n, elapsed);
    }
}

/* O(n²): Quadratic growth */
void demonstrateQuadraticGrowth(void) {
    printf("\n=== O(n^2) Quadratic Growth ===\n");

    for (int n = 100; n <= 1000; n *= 3) {
        clock_t start = clock();

        /* Simulate n² operations */
        volatile int sum = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                sum += i * j;
            }
        }

        clock_t end = clock();
        double elapsed = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;

        printf("n = %7d: %.3f ms (quadratic)\n", n, elapsed);
    }
}

/* O(2ⁿ): Exponential growth */
void demonstrateExponentialGrowth(void) {
    printf("\n=== O(2^n) Exponential Growth ===\n");

    for (int n = 5; n <= 20; n += 5) {
        clock_t start = clock();

        /* Simulate 2^n operations (limited) */
        volatile int sum = 0;
        for (int i = 0; i < (1 << n) && i < 1000000; i++) {
            sum += i;
        }

        clock_t end = clock();
        double elapsed = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;

        printf("n = %2d: %.3f ms (exponential, limited to 1M ops)\n", n, elapsed);
    }
}

/* Demonstrates growth comparison table */
void demonstrateGrowthComparison(void) {
    printf("\n=== Growth Comparison Table ===\n");

    printf("%-10s %-10s %-10s %-12s %-10s %-12s %-10s\n",
           "n", "log n", "n", "n log n", "n²", "n³", "2ⁿ");

    int nValues[] = {10, 100, 1000, 10000, 100000};

    for (int i = 0; i < 5; i++) {
        int n = nValues[i];
        printf("%-10d %-10.0f %-10d %-12.0f %-10d %-12.0f %-10.0f\n",
               n,
               log2(n),
               n,
               n * log2(n),
               n * n,
               pow(n, 3),
               pow(2, n));
    }
}

Explanation of Growth Rate Examples

  1. O(1): Same time regardless of input size.
  2. O(log n): Grows slowly (binary search steps).
  3. O(n): Linear growth (single loop).
  4. O(n log n): Nested loop with logarithmic inner loop.
  5. O(n²): Nested loops (quadratic).
  6. O(2ⁿ): Exponential growth (limited for demonstration).
  7. Comparison table: Shows how growth rates differ.

Expected Output (partial)

=== Order of Growth Demo ===

--- Menu ---
1. O(1) - Constant
2. O(log n) - Logarithmic
3. O(n) - Linear
4. O(n log n) - Linearithmic
5. O(n^2) - Quadratic
6. O(2^n) - Exponential
7. Growth Comparison Table
8. Exit
Enter choice: 2

=== O(log n) Logarithmic Growth ===
For n =      10, log2(n) =  3 steps
For n =     100, log2(n) =  6 steps
For n =    1000, log2(n) =  9 steps
For n =   10000, log2(n) = 13 steps
For n =  100000, log2(n) = 16 steps
For n = 1000000, log2(n) = 19 steps

--- Menu ---
...
Enter choice: 7

=== Growth Comparison Table ===
n          log n      n          n log n     n²         n³          2ⁿ
10         3          10         33          100        1000        1024
100        6          100        664         10000      1000000     1.2676506002282294e+30
1000       9          1000       9965        1000000    1000000000  inf
10000      13         10000      132877      100000000  inf         inf
100000     16         100000     1660964     10000000000 inf        inf

Constants That Mislead

1. Ignoring Constants Incorrectly

While Big-O ignores constants, they matter for small inputs.

/* O(n) with large constant may be slower than O(n²) for small n */

Solution: Consider constants for small input sizes.

2. Confusing Growth Rate Names

Mixing up logarithmic and linear growth.

Solution: Memorize the hierarchy: O(1) < O(log n) < O(n) < O(n log n) < O(n²).

3. Not Considering Space

Focusing only on time while ignoring space complexity.

Solution: Analyze both time and space.

4. Recursive Overhead

Recursive solutions may have hidden stack space complexity.

Solution: Consider stack usage in recursive algorithms.

5. Overlooking Amortized Costs

Some operations occasionally expensive but average out.

Solution: Use amortized analysis for dynamic data structures.

6. Wrongly Analyzing Nested Loops

Assuming all nested loops are O(n²) when inner loop is independent.

Solution: Analyze each loop separately.

7. Integer Overflow in Growth Calculations

Growth calculations can overflow for large n.

Solution: Use double or long long for calculations.

8. Not Testing with Actual Data

Theoretical analysis may not match real-world performance.

Solution: Profile actual code with realistic data sizes.

9. Exponential Algorithms for Large Inputs

Using O(2ⁿ) algorithms for large inputs causes impractical runtime.

Solution: Find polynomial alternatives or use dynamic programming.

10. Memory Considerations

  • O(1) space: Constant extra memory.
  • O(n) space: Linear extra memory.
  • O(n²) space: Quadratic extra memory.
  • Recursive algorithms use O(depth) stack space.

Order of Growth Summary

Order of growth is a crucial concept for comparing algorithm efficiency. It focuses on the dominant term that determines long-term behavior, enabling effective algorithm selection.

Key Takeaways

  • Order of growth describes how resource requirements scale with input size.
  • Ignore lower-order terms and constants.
  • Standard hierarchy: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ).
  • Compare algorithms by their growth rates.
  • Consider both time and space complexity.

Best Practices

  1. Always analyze order of growth for algorithms.
  2. Consider worst-case scenarios.
  3. Balance time and space trade-offs.
  4. Use efficient algorithms for large inputs.
  5. Test with actual data sizes.
  6. Document complexity in comments.
  7. Profile performance when optimization matters.
  8. Use iterative approaches when stack space is limited.
  9. Learn standard algorithm complexities.
  10. Practice analyzing various algorithms.

By mastering order of growth, you’ll make informed decisions about algorithm selection and write efficient, scalable code.

Lessons are also marked complete when you click Next.