Arrays

A collection of elements stored in contiguous memory locations.

Types of Arrays

Static Array

A static array has a fixed size defined at creation and cannot grow or shrink during runtime.

10
0
20
1
30
2
40
3
4
Dynamic Array

A dynamic array can resize automatically as elements are added or removed, allowing flexible storage.

10
0
20
1
30
2
40
3
...
1D Array

A 1D array is a linear collection of elements stored in a single row.

10
0
20
1
30
2
40
3
2D Array

A 2D array stores elements in rows and columns like a table or matrix.

1
0,0
2
0,1
3
1,0
4
1,1
3D Array (2x2x2)

A 3D array stores elements in multiple layers, where a 2×2×2 array has 2 layers each containing a 2×2 grid.

Layer 0

1
0,0,0
2
0,0,1
3
0,1,0
4
0,1,1

Layer 1

5
1,0,0
6
1,0,1
7
1,1,0
8
1,1,1

Traversal

10
0
20
1
30
2
40
3
50
4

Time Complexity: O(n)

Access

10
0
20
1
30
2
40
3
50
4

Time Complexity: O(1)

Insertion

10
0
20
1
40
2
50
3
4

Time Complexity: O(n) due to shifting elements.

Deletion

10
0
20
1
30
2
40
3
50
4

Time Complexity: O(n) due to shifting elements.

Update

10
0
20
1
30
2
99
3
50
4

Time Complexity: O(1)

Search

Linear Search (O(n))

Checks elements sequentially until the target is found.

10
0
50
1
30
2
70
3
20
4
Binary Search (O(log n))

Repeatedly divides a sorted array to find a target efficiently.

10
0
20
1
30
2
50
3
70
4
Low: 0Mid: -High: 4

When to Use Arrays

  • When you need fast random access to elements using an index (O(1)).
  • For storing a collection of elements of the same data type, ensuring memory efficiency.
  • Arrays are commonly used to implement other data structures like stacks, queues, and heaps due to efficient indexed memory access.

Time Complexity Summary

OperationTime ComplexityNotes
AccessO(1)
UpdateO(1)
TraversalO(n)Visit every element
SearchO(n) / O(log n)Linear scan / Binary search (if sorted)
InsertionO(n)Requires shifting elements
DeletionO(n)Requires shifting elements