Arrays

Arrays are fundamental data structures that store a collection of elements in a contiguous block of memory. They provide efficient random access to elements using indices and form the foundation for many other data structures.

Interactive Visualization

Array Operations

Access Element

Highlight element at index

Insert Element

Add element at index

Delete Element

Remove element at index

Array Visualization

arr[0] 10
0
arr[1] 25
1
arr[2] 34
2
arr[3] 47
3
arr[4] 52
4
arr[5] 68
5
arr[6] 73
6

Array Length: 7

Valid Indices: 0 to 6

How Array Operations Work:

Access (O(1)):

Direct access to any element using its index. Arrays provide constant-time access because elements are stored in contiguous memory.

Insert (O(n)):

Inserting at any position requires shifting all subsequent elements to the right. Insertion at the end is O(1).

Delete (O(n)):

Deleting requires shifting all subsequent elements to the left to fill the gap. Deletion from the end is O(1).

Key Characteristics

Memory Layout

  • Contiguous Memory: Elements stored in adjacent memory locations
  • Fixed Size: Size determined at declaration (in some languages)
  • Homogeneous: All elements must be of the same data type
  • Index-based: Elements accessed using zero-based indexing

Performance Features

  • O(1) Random Access: Direct access to any element
  • Cache Friendly: Sequential access benefits from CPU cache
  • Memory Efficient: No extra memory overhead for pointers
  • Predictable: Constant time for access operations

Complexity Analysis

Operation Average Case Worst Case Description
Access O(1) O(1) Direct access using index calculation
Search O(n) O(n) Linear search through elements
Insertion O(n) O(n) Shifting elements for insertion (O(1) at end)
Deletion O(n) O(n) Shifting elements after deletion (O(1) at end)

Array Operations in Different Languages

Explore how arrays are implemented and manipulated in Python, Java, and C++. Each language has its own approach to array handling and memory management.

# Array operations in Python using lists
# Python lists are dynamic arrays

# Declaration and initialization
numbers = [10, 25, 34, 47, 52, 68, 73]
empty_array = []
sized_array = [0] * 5  # Creates [0, 0, 0, 0, 0]

# Access elements (O(1))
first_element = numbers[0]        # 10
last_element = numbers[-1]        # 73
middle_element = numbers[3]       # 47

# Insertion operations
numbers.append(85)                # Add to end: O(1) amortized
numbers.insert(2, 30)            # Insert at index 2: O(n)
numbers.extend([90, 95])          # Add multiple elements: O(k)

# Deletion operations
removed = numbers.pop()           # Remove last: O(1)
removed = numbers.pop(2)          # Remove at index 2: O(n)
numbers.remove(47)                # Remove first occurrence: O(n)

# Search operations
index = numbers.index(34)         # Find index of value: O(n)
exists = 25 in numbers           # Check if value exists: O(n)

# Array properties
length = len(numbers)             # Get array length
is_empty = len(numbers) == 0     # Check if empty

print("Array:", numbers)
print("Length:", length)

Common Use Cases

Data Storage

Store collections of similar data like scores, temperatures, or inventory counts.

Image Processing

Represent images as 2D arrays of pixels for graphics and computer vision applications.

Lookup Tables

Fast constant-time lookups for mathematical functions or mapping relationships.

Matrix Operations

Mathematical computations in scientific computing and machine learning.

Buffer Management

Temporary storage for streaming data, audio/video processing, and I/O operations.

Algorithm Building Block

Foundation for other data structures like heaps, hash tables, and dynamic arrays.