Overview & Concept
An Array is a fundamental linear data structure consisting of a contiguous block of memory allocated to store elements of homogenous or heterogeneous types. Each element in the array is uniquely addressable via its 0-based numerical offset (index). Because memory is laid out sequentially without pointer indirection, accessing any element takes deterministic O(1) time based on the hardware address formula: base_address + index × element_size.
How It Works
Contiguous Physical Memory: All elements occupy uninterrupted consecutive RAM addresses, maximizing CPU L1/L2 cache locality through hardware prefetching.
Direct Index Calculation: Element memory address is calculated instantaneously in hardware arithmetic without traversing intermediate links.
Dynamic Shifting on Insert: Inserting at index k requires shifting all subsequent elements from index n-1 down to k one slot to the right (O(n) time).
Gap Fill on Delete: Deleting at index k leaves a vacancy requiring all subsequent elements to shift left by one slot (O(n) time).
Step-by-Step Execution
Asymptotic Complexity Analysis
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Complexity Bound | O(1) access | O(n) insert/delete | O(n) full shift | O(n) |
| Explanation | Optimal input arrangement minimizing operations. | Expected time for uniformly distributed randomized inputs. | Adversarial degenerate conditions requiring full traversal. | Auxiliary memory allocated beyond original input size. |
Pseudocode & Invariants
// Array Primitive Operations
operation insert(index, value):
for i = length - 1 down to index: // Shift elements right
arr[i + 1] = arr[i]
arr[index] = value // Write into created slot
length = length + 1
operation delete(index):
for i = index to length - 2: // Shift elements left
arr[i] = arr[i + 1]
length = length - 1
operation access(index):
return memory[base + index * size] // Direct O(1) calculation- Contiguity Invariant: For all i where 0 ≤ i < length, element address = base + (i * size).
- Density Invariant: Elements occupy indices 0 through length-1 without uninitialized gaps.
- Boundary Invariant: Any access outside [0, length-1] results in buffer safety boundary enforcement.
Advantages & Disadvantages
Key Advantages
- •Instant O(1) random access by index via direct address calculation.
- •Exceptional CPU cache friendliness and spatial memory locality.
- •Zero pointer memory overhead compared to linked nodes.
- •Predictable fixed-size sequential storage for homogeneous primitive types.
Trade-offs & Limitations
- •Expensive O(n) worst-case insertion and deletion requiring sequential shifting.
- •Fixed capacity in primitive arrays requiring O(n) resize reallocation and copying.
- •Requires contiguous physical memory allocation, which can cause fragmentation in memory-constrained environments.
Real-World Applications
CPU Cache Lines & Hardware Buffers
Underlying hardware register files, DMA ring buffers, and GPU vertex arrays.
Matrix Math & Scientific Computing
NumPy tensors, BLAS libraries, and neural network weights matrix layouts.
Database Disk Block Pointers
Table record ID indexes and sequential scan blocks in relational databases.
Rendering Framebuffers
Pixel color data stored contiguously for high-throughput raster display refresh.
Decision Guide: When to Use
Ideal Scenarios
- ✓When element lookups by index are overwhelmingly frequent.
- ✓When collection size is known beforehand or changes infrequently.
- ✓When maximizing CPU cache throughput and memory efficiency is paramount.
Consider Alternatives When
- ✕When insertions and deletions frequently occur at the beginning or middle of the sequence.
- ✕When total item count is entirely unpredictable and resizing copies are prohibitive.
Explore Related Algorithms
Compare trade-offs and complexity across complementary tools in AlgoViz.