Data Structures · Visualization
Array: Introduction
Array
An array is a collection of items stored in one continuous block of memory, one right after another. Time: O(1) to read/update by index, O(n) to search by value. Space: O(n).
Big-O: Random access O(1); insert/delete in the middle O(n).
Array: Introduction
ArrayAn array is a collection of items stored in one continuous block of memory, one right after another. Because every item sits at a known distance from the start, the computer can jump straight to any position using its index instead of searching for it. Indexing usually starts at 0, so the first item is at index 0, the second at index 1, and so on.
How Array: Introduction Works
- Reserve a block of memory big enough to hold all the elements.
- Store each element in order, side by side, with no gaps.
- Give each element an index based on its position, starting at 0.
- To read a value, calculate its memory location directly from the index (no searching needed).
Array: Introduction Worked Example
Input: Create an array of 4 exam scores: [88, 92, 75, 100]
- score[0] = 88
- score[1] = 92
- score[2] = 75
- score[3] = 100
- Accessing score[2] jumps directly to the 3rd slot and returns 75.
Output: score = [88, 92, 75, 100], score[2] = 75
Press ▶ Run to watch it step by step.
# Array: contiguous, indexable storage
arr = [88, 92, 75, 100]
# Random access is O(1)
print(arr[2]) # 8
for v in arr: # iterate O(n)
print(v, end=" ")
# example: intro on [88, 92, 75, 100]Array: Introduction Time & Space Complexity
When to Use Array: Introduction
- Storing a fixed or predictable list of items, like days of the week
- Building the underlying storage for other structures (stacks, heaps, hash tables)
- Any time you need fast, index-based access to elements
- Image pixels, matrices, and lookup tables
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.