Arrays Notes
Arrays Notes
You have many values of the same kind Store them together under one name
You want to access by position Use indexes like arr[0], arr[1], etc.
Concept Explanation
Fast access, slow insertion Great for lookup, not great for insert/delete
Contiguous Memory All elements are stored next to each other. No gaps.
Once declared, the size of the array can’t change. (In low-level/static
Fixed Size
languages)
All elements must be of the same data type so the memory layout is
Same Type
predictable.
Constant-Time
You can access any element in O(1) time using a formula.
Access
🧠 Big Picture:
When you ask for arr[3], the computer doesn’t "look" for it.
It calculates where it is — instantly — using a simple formula:
address_of(arr[i]) = base_address + (i × size_of_each_element)
That’s the secret sauce behind why arrays are super fast when accessing elements.
Let’s understand this fully.
So when you write arr[3], the CPU doesn’t search. It just does:
jump_to(1000 + 3 × 4) → jump_to(1012)
That’s why we say array access is O(1) (constant time).
🔁 Deep Summary:
Repeat to yourself:
“Arrays allow constant-time access because every element lives at a predictable address,
calculated by the formula:
base + (index × size).”
This is machine-level efficiency — you now understand how CPUs read arrays under the
hood.
After shifting:
[10] [20] [ ] [30] [40] [50]
↑ shift these one step right
So you move:
50 to the right
40 to the right
30 to the right
Now insert 25.
Final array:
text
CopyEdit
[10] [20] [25] [30] [40] [50]
That’s 3 shifts just to insert 1 element.
If there are n elements, you might shift up to n elements in the worst case.
Language Implementation
C int arr[5];
1 1 ❌ OK
2 2 ✅ Reallocate to size 2
3 4 ✅ Resize to 4
5 8 ✅ Resize to 8
So, while each resize is expensive, they happen less and less often.
Types of Arrays
One-dimensional, Two-dimensional, Multi-dimensional, and Jagged Arrays — all explained
deeply and practically.
🎓 Real-World Analogy:
A row of mailboxes in an apartment building:
Each box has a number (0, 1, 2…)
You access each box with its number — arr[0], arr[1]...
📍 Real-Time Applications:
List of names
Queue of customers
Stack of browser history
Sensor readings over time (e.g. temperature over 24 hours)
📍 Real-Time Applications:
Game boards (Tic Tac Toe, Sudoku, Chess)
Matrices in mathematics
Image pixels (grayscale image = 2D array of intensity values)
Seating arrangements
🎓 Analogy:
A bookshelf with:
Rows of shelves (2D)
Multiple racks of shelves (3D)
📍 Real-Time Applications:
3D graphics (storing x, y, z positions)
Volumetric data (MRI scans, simulations)
Deep learning tensors (input to neural networks)
Geographic systems (latitude, longitude, altitude)
🎓 Analogy:
A bookshelf where each row can hold a different number of books.
📍 Real-Time Applications:
Storing variable-length student test scores
Tree-like structures
Triangular matrices in math
Sparse data (data with many missing/empty entries)
🧠 Summary Table
Cube / higher-order
Multi-D Array arr[i][j][k]... Image processing, simulations
tensor
🧠 What Is It?
Linear search means you start at the first element and check each element one by one until
you find the target or reach the end.
🚶 Analogy:
Imagine you lost your keys on a long table covered with books. You look from left to right,
checking every book until you find your keys.
🧰 How It Works:
For array arr = [3, 8, 2, 5, 9] to find 5:
Check arr[0] → 3? No
Check arr[1] → 8? No
Check arr[2] → 2? No
Check arr[3] → 5? Yes! Stop.
Time Complexity:
Best case: O(1) — if target is at first index
Worst case: O(n) — if target is at last index or not present
Average: O(n/2), which is simplified to O(n)
📌 When To Use?
When array is unsorted
When data is small
When simplicity is more important than speed
🧠 What Is It?
Binary search works only on sorted arrays.
It:
Compares target with the middle element
If target is smaller, search left half
If target is larger, search right half
Repeat until found or sub-array is empty
🧰 How It Works:
Given sorted array: arr = [1, 3, 5, 7, 9, 11, 13], find 7.
Step 1: middle is arr[3] = 7 → found instantly!
If looking for 6:
Compare with middle (7): 6 < 7 → search left half [1, 3, 5]
Middle of left half: 3
6 > 3 → search right half [5]
Check 5, not 6, stop — not found.
⏱ Time Complexity:
Best case: O(1)
Worst case: O(log n) (halving each time)
Average case: O(log n)
📌 When To Use?
When array is sorted
When performance matters (large datasets)
Widely used in databases, search engines
🎓 Exercise:
Given sorted array:
arr = [2, 4, 6, 8, 10, 12, 14]
Find 10 using binary search:
What are the steps?
Array Length and Memory Allocation in Static Arrays
4️⃣ Example in C:
int arr[5]; // Allocates memory for 5 integers at once
The compiler reserves 5 * sizeof(int) bytes.
You can access elements from arr[0] to arr[4].
Trying to access arr[5] or beyond causes undefined behavior.
Topic Details
🧠 Summary Table