Dynamic Arrays

Nathan Sprague

9/8/2021

OpenDSA AList

java.util.ArrayList

Dynamic Arrays

ArrayList add Analysis

ArrayList add Analysis

ArrayList add Analysis

Let’s time this to see if that gives any insight…

Reminder… Geometric Series

Socrative Quiz (Summation Warm-up)

Cost of Append in Dynamic Array

Solving the Summation

\[ \begin{aligned} n + \sum_{i=0}^{\lceil \log_2 n \rceil-1} 2^{i} &=n + 2^{\lceil \log_2 n \rceil-1+1} - 1\\ &=n + 2^{\lceil \log_2 n \rceil} - 1 \\ &\leq n + 2^{ \log_2 n +1} - 1 \\ & = n + 2n - 1\\ &= 3n - 1 \leq 3n \end{aligned} \]

Amortized cost of append

Socrative Quiz…

You are performing a code review on the following Java method. This is part of the software that manages an automobile’s safety features. loggingList is an ArrayList that stores log messages that will eventually be saved to persistent storage. Which of the following is the most appropriate evaluation of this code?

public static void respondToCollision(Area area) {

    loggingList.add("Airbag Depolyed!");
    if (area == Area.FRONT)
        deployAirbag();
}
  1. LGTM
  2. Spelling error in log message.
  3. area == Area.FRONT should be area.equals(Area.FRONT)
  4. Efficiency issues could lead to loss of life.
  5. Problem with code style: Missing braces after if.

Bankers View (informal)

What about insert (not append)?