CS 70

Best- and Worst-Case Analysis

Another Example

bool lowerHalf(float x, const float* arr, size_t arrSize) {  // 1
    size_t numLessThan = 0;                                  // 2
    for (size_t i = 0; i < arrSize; ++i) {                   // 3
        if (arr[i] < x) {                                    // 4
            ++numLessThan;                                   // 5
        }                                                    // 6
    }                                                        // 7
    return numLessThan < arrSize/2.0;                        // 8
}                                                            // 9

This time, let's let

  • \( n \) be the number of elements in the array and
  • \( \mathrm{C}_{\mathrm{inc}}(n) \) be the number of increments the algorithm performs.
  • Horse speaking

    Whoah, hold on. I don't think I can do that analysis.

  • LHS Cow speaking

    Why not?

  • Horse speaking

    Because even if I know how big the array is, I can't figure out how many increments there will be!

  • LHS Cow speaking

    Oh, that's true! It also depends on the value of x and the contents of the array.

  • RHS Cow speaking

    Can we at least come up with a range of possibilities?

Sometimes our quantity \( n \) does not capture all of the relevant properties of the input.

When that's true, it is common to imagine a range of possibilities for a given \( n \):

Best-Case Analysis: What is the smallest number of operations that will be performed as a function of \( n \)?

Worst-Case Analysis: What is the largest number of operations that will be performed as a function of \( n \)?

Best-Case Analysis

Let's let

  • \( n \) be the number of elements in the array, and
  • \( \mathrm{C}_{\mathrm{best-inc}}( n ) \) be the smallest number of increment operations the algorithm will perform for an array of size \( n \).

Work out the best-case number of increments as an expression in terms of the number of elements in the array.

Remember that there are two increments in the loop body.

  • ++i on line 3
  • ++numLessThan on line 5, inside the if statement

and that we are trying to find the path through the code that results in the fewest number of increments.

Looking at the code, we can see…

Line(s)
1–2 There are no increments on these lines.
3–7 Here we have a loop, which is run \( n \) times.
In each iteration of the loop, we definitely perform one increment on the loop variable.
In the best case, x is smaller than all elements in the array. In that case, we never perform the increment on line 5.
So we can characterize the number of increments in the loop as \( \sum^{n}_{i=1} 1 = n \).
8 There are no increments on line 8.

All told, that gives us the best-case number of increments as \( \mathrm{C}_{\mathrm{best-inc}}( n ) = n \), where \( n \) is the size of the array.

Here's a video to walk you through the analysis:

Worst-Case Analysis

Let's let

  • \( n \) be the number of elements in the array and
  • \( \mathrm{C}_{\mathrm{worst-inc}}(n) \) be the largest number of increment operations the algorithm will perform for an array of size \( n \).

Work the worst-case number of increments as an expression in terms of the number of elements in the array.

Remember that there are two increments in the loop body.

  • ++i on line 3
  • ++numLessThan on line 5, inside the if statement

and that we are trying to find the path through the code that results in the largest number of increments.

Looking at our code, we can see

Line(s)
1–2 No increments on these lines.
3–7 A loop that runs \( n \) times.
In each iteration of the loop, we definitely perform one increment on the loop variable.
In the worst case, x is larger than all elements in the array. In that case, we always perform the increment on line 5.
So we can characterize the number of increments in the loop as \( \sum^{n}_{i=1} 2 = 2n \).
8 There are no increments on line 8.

That analysis gives us the worst-case number of increments as \( \mathrm{C}_{\mathrm{worst-inc}}(n) = 2n \), where \( n \) is the size of the array.

A video to walk through the analysis:

Conclusion

From our analysis, here are some statements we can safely make, where \( n \) is the size of the array:

  • In the best case, the algorithm performs \( n \) increments.
  • In the worst case, the algorithm performs \( 2n \) increments.

And from this information we can say the following about the algorithm as a whole:

  • It performs at least \( n \) increments.
  • It performs no more than \( 2n \) increments.

(When logged in, completion status appears here.)