Skip to main content
Engineering LibreTexts

2: Array-Based Lists

  • Page ID
    8436
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this chapter, we will study implementations of the List and Queue interfaces where the underlying data is stored in an array, called the backing array. The following table summarizes the running times of operations for the data structures presented in this chapter:

    \(\mathtt{get(i)}\)/ \(\mathtt{set(i,x)}\) \(\mathtt{add(i,x)}\)/ \(\mathtt{remove(i)}\)
    ArrayStack \(O(1)\) \(O(\mathtt{n}-\mathtt{i})\)
    ArrayDeque \(O(1)\) \(O(\min\{\mathtt{i},\mathtt{n}-\mathtt{i}\})\)
    DualArrayDeque \(O(1)\) \(O(\min\{\mathtt{i},\mathtt{n}-\mathtt{i}\})\)
    RootishArrayStack \(O(1)\) \(O(\mathtt{n}-\mathtt{i})\)

    Data structures that work by storing data in a single array have many advantages and limitations in common:

    • Arrays offer constant time access to any value in the array. This is what allows \(\mathtt{get(i)}\) and \(\mathtt{set(i,x)}\) to run in constant time.
    • Arrays are not very dynamic. Adding or removing an element near the middle of a list means that a large number of elements in the array need to be shifted to make room for the newly added element or to fill in the gap created by the deleted element. This is why the operations \(\mathtt{add(i,x)}\) and \(\mathtt{remove(i)}\) have running times that depend on \(\mathtt{n}\) and \(\mathtt{i}\).
    • Arrays cannot expand or shrink. When the number of elements in the data structure exceeds the size of the backing array, a new array needs to be allocated and the data from the old array needs to be copied into the new array. This is an expensive operation.

    The third point is important. The running times cited in the table above do not include the cost associated with growing and shrinking the backing array. We will see that, if carefully managed, the cost of growing and shrinking the backing array does not add much to the cost of an average operation. More precisely, if we start with an empty data structure, and perform any sequence of \(m\) \(\mathtt{add(i,x)}\) or \(\mathtt{remove(i)}\) operations, then the total cost of growing and shrinking the backing array, over the entire sequence of \(m\) operations is \(O(m)\). Although some individual operations are more expensive, the amortized cost, when amortized over all \(m\) operations, is only \(O(1)\) per operation.


    This page titled 2: Array-Based Lists is shared under a CC BY license and was authored, remixed, and/or curated by Pat Morin (Athabasca University Press) .

    • Was this article helpful?