Array Foundation
DSA Arrays Foundation

Array Foundation

What is an array

An array stores elements in a contiguous block of memory so you can jump to any index in O(1). Think of numbered lockers in a straight line. A 2D array is the same idea as a grid — rows × columns — used for matrices and image pixels.

  • 1D array: a linear sequence. Access is arr[i]. Address is base + i × sizeof(type).
  • 2D array: a grid. Access is arr[i][j]. C stores it row-major: base + (i × cols + j) × sizeof(type).
  • Why it is fast: index access is O(1). Sequential scans are cache-friendly.
  • The trade-off: insert or delete in the middle is O(n) because elements must shift. Valid indices are 0 .. n−1.
  • Where it shows up: lookup tables, buffers, matrices, pixels, and almost every algorithm that needs random access.

Try it

Insert, delete, and access cells. Watch indices and memory addresses update. Switch 1D and 2D to see how a grid is laid out in memory.

Approach

1D Array  Linear · Contiguous Memory

A 1D array is a fixed-size, ordered sequence of elements stored at consecutive memory addresses. Every element is the same type, so the CPU can jump to any element instantly using simple arithmetic.

Core Operations

  1. Access — Provide an index i. The CPU computes the address as base + i × size and reads directly. No traversal needed. O(1).
  2. Traversal — Visit every element from index 0 to n−1 in order, one step at a time. O(n).
  3. Insert at middle — To insert at index pos, shift every element from pos to end one step to the right, then place the new value. O(n).
  4. Delete at middle — Remove element at index pos, then shift every element after it one step to the left to close the gap. O(n).
  5. Append / Delete at end — No shifting required; simply place or remove the last element. O(1).

Key Insight

Arrays are fast at the edges (append/pop) and fast at random reads, but expensive in the middle — insertions and deletions force a chain of shifts. The larger the array, the worse the middle operations get.

Memory Layout — arr = [10, 25, 7, 42, 18]

idx [0] [1] [2] [3] [4]
val 10 25 7 42 18
addr 0x1000 0x1004 0x1008 0x100C 0x1010

Highlighted cell [2] = value 7 at address 0x1008 — accessed in O(1) via base + 2×4

2D Array  Grid · Row-Major Order

A 2D array is a grid of rows and columns. Internally, it is still stored as a flat block of memory — rows are laid out one after another (row-major order). You address each cell with two indices: arr[row][col].

Core Operations

  1. Access cell [i][j] — The CPU computes the flat offset as (i × cols + j) × size and reads it directly. Still O(1).
  2. Row traversal — Fix row i, iterate over all columns j = 0 … cols−1. Reads contiguous memory → cache-friendly. O(cols).
  3. Full traversal — Outer loop over rows, inner loop over columns. Visit every cell exactly once. O(n × m).
  4. Update cell — Access by [i][j] in O(1), overwrite the value. No shifting needed since the grid dimensions stay fixed. O(1).
  5. Column traversal — Fix column j, iterate over all rows. Reads non-contiguous memory (jumps by cols each step) → less cache-friendly. O(rows).

Key Insight — Row-Major Order

Even though you think in rows and columns, the computer stores all of row 0 first, then all of row 1, then row 2 — in one long flat sequence. Traversing row by row is always faster than column by column because it reads memory in order, keeping the CPU cache hot.

Grid Visualization — 3×4 matrix · cell [1][2] selected

col 0 col 1 col 2 col 3
row 0 1 2 3 4
row 1 5 6 7 8
row 2 9 10 11 12

Flat in memory (row-major)

1
0,0
2
0,1
3
0,2
4
0,3
5
1,0
6
1,1
7
1,2
8
1,3
9
2,0
10
2,1
11
2,2
12
2,3
Row 0
Row 1
Row 2
[1][2] selected

Cell [1][2] = value 7. Flat offset = (1×4 + 2) × 4 = 24 bytes from base.

Complexity Summary

Operation 1D Array 2D Array
Access by index O(1) O(1)
Traverse all elements O(n) O(n × m)
Insert / Delete at end O(1) N/A (fixed grid)
Insert / Delete at middle O(n) N/A (fixed grid)

Interactive Array Visualizer

arr[ ] — index & value
memory address (base = 0x1000, int = 4 bytes)
Length: 0 Selected idx: — Selected val: —
Rows: 4 Cols: 4 Selected: —

1D Array Controls

Array Stats

Length

8

Memory

32 B

Access

O(1)

Insert

O(n)

Array Representations

1D: index → value


        

2D: [row][col] → value


        

Implementation

1d_array.c

1#include <stdio.h>
2#define MAX 100
3
4int main() {
5// Declaration & Initialization
6int arr[MAX];
7int n = 5;
8int i;
9
10// Assign values
11int data[] = {10, 25, 7, 42, 18};
12for (i = 0; i < n; i++)
13arr[i] = data[i];
14
15// Access element: O(1)
16printf("arr[2] = %d\n", arr[2]);
17
18// Traverse
19for (i = 0; i < n; i++)
20printf("%d ", arr[i]);
21
22// Insert at index: shift right
23int pos = 2, val = 99;
24for (i = n; i > pos; i--)
25arr[i] = arr[i-1];
26arr[pos] = val; n++;
27
28// Delete at index: shift left
29int del = 2;
30for (i = del; i < n - 1; i++)
31arr[i] = arr[i+1];
32n--;
33return 0;
34}

2d_array.c

1#include <stdio.h>
2#define ROWS 3
3#define COLS 4
4
5int main() {
6// 2D array declaration
7int grid[ROWS][COLS] = {
8{1, 2, 3, 4},
9{5, 6, 7, 8},
10{9, 10, 11, 12}
11};
12
13// Access element: O(1)
14printf("grid[1][2] = %d\n", grid[1][2]);
15
16// Traverse 2D array
17for (int i = 0; i < ROWS; i++) {
18for (int j = 0; j < COLS; j++)
19printf("%4d", grid[i][j]);
20printf("\n");
21}
22
23// Memory address of grid[i][j]:
24// base + (i*COLS + j)*sizeof(int)
25printf("&grid[1][2] = %p\n", &grid[1][2]);
26return 0;
27}

Algorithm & Pseudocode

array_operations.pseudo

1// ── INSERT at index pos ──
2ALGORITHM Insert(arr, n, pos, val):
3FOR i = n DOWNTO pos+1 DO
4arr[i] ← arr[i-1] // shift right
5arr[pos] ← val
6n ← n + 1
7RETURN n
8
9// ── DELETE at index pos ──
10ALGORITHM Delete(arr, n, pos):
11FOR i = pos TO n-2 DO
12arr[i] ← arr[i+1] // shift left
13n ← n - 1
14RETURN n
15
16// ── ACCESS at index i ──
17ALGORITHM Access(arr, i):
18address ← base + i × sizeof(element)
19RETURN *address // O(1)
20
21// ── 2D ACCESS at [r][c] ──
22ALGORITHM Access2D(grid, r, c, cols):
23address ← base + (r×cols + c) × sizeof(el)
24RETURN *address // O(1)

complexity_analysis.txt

1// ── TIME COMPLEXITY ──
2
3Access → O(1) // index math
4Search → O(n) // linear scan
5Insert (end) → O(1) // amortized
6Insert (mid) → O(n) // shift elements
7Delete (end) → O(1)
8Delete (mid) → O(n) // shift elements
9
10// ── SPACE COMPLEXITY ──
11
121D Array: O(n)
132D Array: O(n × m)
14
15// ── MEMORY LAYOUT ──
16
17// 1D: Contiguous block
18[0x1000][0x1004][0x1008]...
19 arr[0] arr[1] arr[2]
20
21// 2D (row-major): Rows stored back-to-back
22Row 0: [0][1][2] Row 1: [0][1][2] ...
23// addr = base + (i*cols + j)*4