A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Array Data Structure: Representation and Operations

College Depth 76 in the knowledge graph ☐ I know this ☆ Set as goal
519topics build on this
326prerequisites beneath it
See this on the map →
Arrays and Lists→→Binary SearchList Abstract Data Type: Interface and Semantics
arrays data-structure memory

Core Idea

Arrays store elements in contiguous memory locations, enabling O(1) random access by index. Insertion and deletion away from the end require shifting elements (O(n)). Understanding memory layout, cache locality, and resizing overhead is critical for performance.

How It's Best Learned

Implement insertion and deletion at different positions, measure performance, and reason about why access is fast (address arithmetic) while modification is slow. Compare empirically to linked lists.

Common Misconceptions

Explainer

You already know arrays from programming — you've used them to store lists of values and access elements by index. Now we examine *why* arrays behave the way they do by looking at how they are laid out in memory. An array allocates a single contiguous block of memory, with each element occupying a fixed number of bytes. When you request element at index *i*, the computer calculates the memory address using simple arithmetic: `base_address + i × element_size`. This single multiplication and addition is why array access is O(1) — no searching, no following pointers, just one address computation.

This contiguous layout also gives arrays an enormous hidden advantage: cache locality. Modern CPUs don't fetch individual bytes from main memory; they load entire cache lines (typically 64 bytes) at once. When you access array[0], the CPU loads array[0] through roughly array[15] (for 4-byte integers) into the fast L1 cache. Iterating through the array sequentially hits the cache almost every time, making array traversal far faster in practice than the O(n) notation alone suggests. This is why arrays often outperform linked lists even when both have O(n) traversal — the array's sequential memory access pattern plays to the hardware's strengths.

The cost shows up during insertion and deletion. If you insert an element at position 3 of a 1000-element array, every element from position 3 onward must shift one slot to the right — that's 997 copy operations, making insertion O(n) in the worst case. Deletion works the same way in reverse: removing an element leaves a gap that must be closed by shifting elements left. The only exception is operating at the end of the array, where no shifting is needed. Dynamic arrays (like Python's list or Java's ArrayList) add another consideration: when the array fills its allocated capacity, it must allocate a new, larger block of memory and copy everything over. This resizing is O(n) when it happens, but because the new block is typically double the old size, the amortized cost of appending remains O(1).

Understanding these tradeoffs — O(1) random access and excellent cache performance versus O(n) insertion and deletion — is the foundation for choosing between arrays and other data structures. When your workload is mostly reading and appending, arrays are hard to beat. When your workload involves frequent insertions and deletions at arbitrary positions, you'll want to consider linked lists or tree-based structures, which trade away contiguous memory for pointer-based flexibility.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → While Loops → For Loops → Arrays and Lists → Array Data Structure: Representation and Operations

Longest path: 77 steps · 326 total prerequisite topics

Prerequisites (1)

Leads To (2)