Home
DSA Visualizer
Arrays
Array Operations
Array Operations
Array operations are the four basic ways to work with an array: insert, delete, search and update.
5 / 10 ELEMENTS
READY
0
1
2
3
4
Insert, delete, access or search — and watch which of them have to move the other elements.
Value
Index
Intuition
Arrays are contiguous memory blocks. Accessing an element by index is instant (O(1)), but inserting or deleting elements requires shifting subsequent elements, which can be slow (O(n)).
Concept
- Access (Get/Update): O(1) - Direct memory address calculation.
- Search: O(n) - Linear scan (unless sorted).
- Insertion: O(n) - Requires shifting elements to make space.
- Deletion: O(n) - Requires shifting elements to fill the gap.
How it Works
Insertion at Index i:
- Shift elements from index
itoendone position to the right. - Place the new value at index
i.
Deletion at Index i:
- Shift elements from index
i+1toendone position to the left. - Decrease array size.
Step-by-Step Breakdown
Watch the visualization:
- Notice how elements move during Insert/Delete.
- Observe the immediate highlight for Update.
- See the linear scan for Search.
Trace: A = [10, 20, 30, 40, 50]
get(2)
One address calculation: the base plus two element widths. No element is examined and nothing moves, whatever the length of the array.
30 · 0 moves
search(40)
Compares 10, 20, 30, then 40. A value carries no information about where it lives, so on unsorted data a scan is the only option — sorting first would buy a binary search.
index 3 · 4 reads
insert(1, 15)
A gap has to open at index 1, so 50, 40, 30 and 20 each move one slot right, in that order — right to left, or they would overwrite each other. Then 15 is written into the hole.
4 moves
state
Six elements now, and the array had to have room for the sixth. A fixed-size array would have needed a reallocation and a full copy here.
[10, 15, 20, 30, 40, 50]
delete(0)
Removing the first element leaves a hole at the front, so all five survivors move one slot left. This is the worst case for deletion: every remaining element moves.
5 moves
state
Back to five elements.
[15, 20, 30, 40, 50]
the pattern
Moves at the front are N, moves at the back are 0. Push and pop at the end are effectively O(1), which is why a stack on an array is a good fit and a queue on one is not — a queue takes from the front, so every dequeue would shift the whole array. A circular buffer fixes that by moving the index instead of the data.
O(N) at the front
When to Use
- Fast access by index is required.
- Size is known or fixed (static arrays).
- Simple data storage.
When NOT to Use
- Frequent insertions/deletions in the middle (use Linked List).
- Unknown size (use Dynamic Array / Vector).
How to Identify
Problems involving sequences, indices, or fixed-size collections.
Applying for software engineering roles? Free, no sign-up:
