Arrays

An array is a collection of similar data elements stored at contiguous memory locations.

Fundamental Concepts

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
...
Indexed Array

An indexed array stores elements that are accessed using numerical indices like 0, 1, 2.

10
0
20
1
30
2
40
3
Associative Array

An associative array stores data as key–value pairs, where elements are accessed using keys instead of indices.

"name"
:
"Alice"
"age"
:
30
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

Arrays can be structured in multiple ways to store data.

Traversal

10
0
20
1
30
2
40
3
50
4

Traversal means visiting each element one by one, and its time complexity is O(n).

Access

10
0
20
1
30
2
40
3
50
4

Access means retrieving an element by index, and its time complexity is O(1).

Insertion

10
0
20
1
40
2
50
3
4

Insertion means adding an element at a position, and its time complexity is O(n) due to shifting elements.

Deletion

10
0
20
1
30
2
40
3
50
4

Deletion means removing an element, and its time complexity is O(n) due to shifting elements.

Update

10
0
20
1
30
2
99
3
50
4

Update means modifying the value at a specific index, and its time complexity is O(1).

Search

Linear Search

Linear search checks elements sequentially until the target is found, with time complexity O(n).

10
0
50
1
30
2
70
3
20
4
Binary Search

Binary search repeatedly divides a sorted array to find a target efficiently, with time complexity O(log n).

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

Search means finding a value in the array, and its time complexity is O(n) in general.

Sorting (Bubble Sort)

50
0
30
1
70
2
20
3
10
4

Bubble sort repeatedly swaps adjacent elements until the array is sorted, with time complexity O(n²).

Merge

10
30
+
20
40
=

Merge combines two sorted arrays into a single sorted array, typically taking O(n + m) time.

Time Complexity Comparison

OperationStatic ArrayDynamic ArrayIndexed Array (1D/2D/3D)Associative Array
AccessO(1)O(1)O(1)O(1) avg
TraversalO(n)O(n)O(n)O(n)
SearchO(n)O(n)O(n)O(1) avg
Insert (middle)O(n)O(n)O(n)O(1) avg
DeleteO(n)O(n)O(n)O(1) avg