B2.2.1Compare static and dynamic data structures
0/8
B2.2 Data structuresB2.2.1

Compare static and dynamic data structures

Compare static and dynamic data structures

SL + HL≈ 45 minCommand term: CompareFree lesson

In this lesson

  • Describing static data structures (arrays, Python tuples) and dynamic data structures (ArrayList, Python lists)
  • Explaining how a dynamic list grows in memory
  • Comparing the two in flexibility, memory use and speed
  • Choosing a suitable structure for a given situation

LearnTwo kinds of data structure

    0 of 5

    CodeA fixed size and a growing list

    scores = (12, 0, 0)
    print("tuple length:", len(scores))
    try:
    scores.append(15)
    except AttributeError:
    print("cannot add a 4th score to the tuple")
    marks = []
    for i in range(4):
    marks.append(10 + i)
    print("list size:", len(marks))
    Output
    tuple length: 3
    cannot add a 4th score to the tuple
    list size: 4

    The array (or tuple) cannot take a fourth item; the list grows as items are added.

    LearnHow a dynamic list grows

      0 of 3

      LabSeats and snacks

      Sell tickets for a concert with six seats, and take snack orders in a list that grows.

      Static: concert seats

      seats = [None] * 6 # used as a fixed-size array
      empty0
      empty1
      empty2
      empty3
      empty4
      empty5

      Places in memory: 6 · used: 0

      Dynamic: snack orders

      orders = []
      empty0
      empty1
      empty2
      empty3

      Items: 0 · places allocated: 4 · times grown: 0 · items copied so far: 0

      Python has no built-in fixed-size array; a tuple has a fixed size but its items cannot be changed at all. In this simplified model the list doubles its storage when full; Java’s ArrayList grows by about 1.5 times and Python lists by a smaller amount, but the idea is the same. To complete this activity, try to sell more tickets than there are seats, and add enough orders for the list to grow twice.

      LearnComparing static and dynamic structures

      Static (array, tuple)Dynamic (ArrayList, Python list)
      SizeFixed when created; must be known (or estimated) in advanceGrows and shrinks as needed; the size need not be known
      MemoryReserves space for every element, even unused ones; no extra memory beyond thatUses memory roughly in line with the number of items, plus spare capacity (or a reference per node)
      SpeedDirect access by index; never pays for resizingIndex access is also direct for array-based lists, but growing means copying items; linked structures must be followed item by item
      RiskCannot hold more than its size: extra items are rejected (e.g. an index error)Can keep growing until memory runs out

      CheckQuick check

      Which of these is a similarity between a Java array and an ArrayList (or a Python tuple and a list)?

      CheckStatic or dynamic?

      Which kind of structure suits each set of data better?

      1. The 64 squares of a chessboard
      2. Messages arriving in a chat
      3. The lessons in each of the 5 periods of a school day
      4. The songs a user adds to a playlist
      5. The names of the players who join an open online game
      6. The red, green and blue values of one pixel

      CheckQuick check

      A program creates an array of 1,000 elements to store sensor readings, but on most days only 40 readings arrive. What is the main disadvantage?

      ExamWorked exam answer

      A program stores the goals scored by each of the 11 players in a football team during a match. Explain whether a static or a dynamic data structure is more suitable.

      [3]
        Try writing your own answer first.

        ReviewMistakes examiners see

        Misconception

        Saying a dynamic structure is always better. If the size is known and fixed, a static structure is simpler and wastes nothing.

        Misconception

        Claiming that array-based lists cannot be accessed by index. ArrayList and Python lists support direct index access; it is linked structures that must be followed item by item.

        Misconception

        Answering a “Compare” question with differences. Compare asks for similarities, referring to both items throughout; “Compare and contrast” asks for similarities and differences.

        Exam tip

        Link each point to the scenario: is the number of items known? Does it change? How often are items accessed?

        Exam practiceExam-style questions

        3 questions · 11 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.

        Scenario

        An online bookshop program keeps each customer’s basket of books while they shop, and stores the total sales for each of the 12 months of the year.

        (a)Explain · AO3[3]

        Explain which kind of data structure is more suitable for storing the books in a customer’s basket.

        (b)Compare and contrast · AO3[4]

        Compare and contrast storing the 12 monthly sales totals in a static structure (a Java array, or a Python tuple) and in a dynamic structure (an ArrayList, or a Python list). The totals are updated after every sale.

        (c)Explain · AO3[4]

        A basket is stored in a dynamic list that starts with room for 5 items and doubles its capacity whenever it is full. A customer adds 12 books, one at a time. State how many times the list grows and how many items are copied in total, and explain why growing by doubling is better than growing by one place at a time.

        0 / 11

        0 of 3 questions marked. Mark every question to see your total.

        Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.