Arrays
Arrays store collections of same-type data in contiguous memory, allowing instant, indexed access. While providing fast lookup, they typically have a fixed size determined at creation, making resizing difficult.
Theory & Explanation
Arrays: Definition, Representation, and Operations
Exam Tip: In exam answers, highlight three core points about an array: all elements share the same data type, elements sit in contiguous (adjacent) memory locations, and any element can be accessed directly using its index.
1. Definition of an Array
An array is a linear data structure that stores a collection of elements of the same data type (such as integers, characters, or floating-point numbers) in contiguous memory locations. Because the elements are placed side by side in memory and each slot has a fixed size, the system can compute the exact memory address of any element from its index.
- Homogeneous elements: Every element has the same data type.
- Contiguous memory allocation: Elements are stored consecutively in memory, enabling direct index-based access.
- Zero-based indexing (in C and Python): For an array of size n, valid indices run from
0ton - 1. - Fixed capacity in static C arrays: Size is set at declaration; dynamic resizing requires extra logic or dynamic allocation.
2. Single-Dimensional (1D) and Multidimensional Arrays
A single-dimensional (1D) array represents a straight line or single list of items indexed by a single subscript, such as arr[i]. A multidimensional array is an array of arrays—most commonly a 2D array arranged in rows and columns like a table or matrix, accessed using two indices mat[i][j] for row i and column j.
Creation and Initialization
C Example (1D and 2D Arrays):
int scores[5]; / 1D array of 5 ints (uninitialized) /
int ages[3] = {20, 22, 19}; / 1D array initialized with 3 values /
int grid[2][3] = { / 2D array: 2 rows, 3 columns /
{10, 20, 30},
{40, 50, 60}
};
Python Example (1D and 2D Lists):
ages = [20, 22, 19] # 1D list
grid = [ # 2D list (list of rows)
[10, 20, 30],
[40, 50, 60]
]
print(grid[1][2]) # Row 1, Col 2 -> 60
1D Array Operations: Creation, Insertion, and Deletion
Creation: Allocating memory for n elements takes O(n) time to initialize all entries (or O(1) just to reserve raw block space without initialization). Accessing an element at index k takes O(1) time using Base + k W, where Base is the starting address and W is the size of one element in bytes.
Insertion at Index pos: To insert a new element at index pos without losing existing data, shift elements from current_size - 1 down to pos one step to the right, place the new value at arr[pos], and increment current_size. Inserting at the end (when space exists) is O(1); inserting at the start or middle takes up to O(n) shifts in the worst case.
C Program — Insertion in a 1D Array:
#include <stdio.h>
int main(void) {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5;
int pos = 2;
int val = 25;
for (int i = n - 1; i >= pos; i--) {
arr[i + 1] = arr[i];
}
arr[pos] = val;
n++;
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/ Expected Output: 10 20 25 30 40 50 /
Python Program — Insertion in a 1D List:
arr = [10, 20, 30, 40, 50]
pos = 2
val = 25
arr.insert(pos, val)
print(arr)
# Expected Output: [10, 20, 25, 30, 40, 50]
Deletion at Index pos: To remove the element at index pos, shift elements from pos + 1 up to n - 1 one step to the left to close the gap, then decrement n. Deleting from the end is O(1); deleting from the beginning or middle takes up to O(n) shifts in the worst case.
C Program — Deletion from a 1D Array:
#include <stdio.h>
int main(void) {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5;
int pos = 1;
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/ Expected Output: 10 30 40 50 /
Python Program — Deletion from a 1D List:
arr = [10, 20, 30, 40, 50]
pos = 1
removed = arr.pop(pos)
print("Removed:", removed)
print("Remaining:", arr)
# Expected Output:
# Removed: 20
# Remaining: [10, 30, 40, 50]
3. Representation of Arrays: Row Major vs. Column Major Order
Computer memory is one-dimensional (linear), so a 2D array of R rows and C columns must be mapped into a single sequence of addresses. Let B be the base address (address of element [0][0] for 0-based indexing) and W be the size of one element in bytes.
Suppose we have a 2 x 3 matrix (R = 2, C = 3) with B = 1000 and W = 4 bytes:
Row 0: [10, 20, 30]
Row 1: [40, 50, 60]
Row Major Order
In Row Major Order (used in C and C++), elements are stored row by row: all of Row 0 first, then Row 1, and so on. In memory, the example above appears as 10, 20, 30, 40, 50, 60.
0-Based Address Formula: Address(i, j) = B + W (i C + j)
Worked Example: Find the address of element 50 at i = 1, j = 1 in the 2 x 3 matrix above (B = 1000, W = 4, C = 3):
Address(1, 1) = 1000 + 4 (1 3 + 1) = 1000 + 4 4 = 1016.
Column Major Order
In Column Major Order, elements are stored column by column: all of Column 0 first, then Column 1, and so on. In memory, the same example appears as 10, 40, 20, 50, 30, 60.
0-Based Address Formula: Address(i, j) = B + W (j R + i)
Worked Example: Find the address of element 50 at i = 1, j = 1 in the same 2 x 3 matrix under Column Major Order (B = 1000, W = 4, R = 2):
Address(1, 1) = 1000 + 4 (1 2 + 1) = 1000 + 4 * 3 = 1012.
4. Quick Summary Table
| Operation / Property | Behavior | Worst-Case Time |
|---|---|---|
| Access by Index | Direct address calculation via offset | O(1) |
| Insertion (Middle / Start) | Shifts elements right to open a slot | O(n) |
| Deletion (Middle / Start) | Shifts elements left to close the gap | O(n) |
| Space Complexity | Stores n elements of size W | O(n) |
