=========================================================
SUBJECT: CS101 - Introduction to Data Structures
TOPIC: The Array
=========================================================
### 1. WHAT IS AN ARRAY?
An array is a fundamental data structure used to store a collection of elements.
The key characteristic of an array is that its elements are stored in
**contiguous** memory locations. This means that if the first element is at memory
address `X`, the next element will be at `X + size_of_element`, and so on.
This structure allows for efficient access to any element if its position (index)
is known.
### 2. CORE CHARACTERISTICS
* **Indexed:** Each element in an array has a numerical index, typically starting
from 0. `array[0]` is the first element, `array[1]` is the second, etc.
* **Homogeneous Elements:** In statically-typed languages (like C++, Java), all
elements in an array must be of the same data type (e.g., an array of integers, an
array of strings).
* **Fixed Size (Static Arrays):** In many languages, the size of the array must be
declared when it is created and cannot be changed later. Dynamic arrays (like
Vectors in C++ or ArrayLists in Java) are abstractions built on top of static
arrays to provide resizing capabilities.
### 3. TIME AND SPACE COMPLEXITY
Understanding the performance of array operations is crucial for technical
interviews.
**Space Complexity:** O(N) - to store N elements.
**Time Complexity of Basic Operations:**
| Operation | Average Case | Worst Case | Explanation
|
|-----------------|--------------|--------------|----------------------------------
----------------------------------------|
| Access (by index)| O(1) | O(1) | Direct memory address
calculation. Extremely fast. |
| Search | O(N) | O(N) | Must potentially check every
element (linear search). |
| Insertion | O(N) | O(N) | To insert at index `i`, all
elements from `i` to the end must be shifted.|
| Deletion | O(N) | O(N) | To delete at index `i`, all
elements after `i` must be shifted left. |
*Note: Insertion at the very end of an array can be O(1) if the array is not full.*
### 4. ADVANTAGES OF ARRAYS
1. **Fast, Constant-Time Access:** `O(1)` read/write time makes arrays ideal when
you need to frequently access elements by their position.
2. **Cache Locality:** Since elements are stored next to each other in memory,
iterating through an array is very fast. The CPU can pre-fetch nearby memory blocks
into the cache, reducing latency.
### 5. DISADVANTAGES OF ARRAYS
1. **Fixed Size:** Static arrays cannot grow or shrink, which can lead to wasted
space or the need to create a new, larger array and copy all elements over.
2. **Slow Insertions and Deletions:** The `O(N)` cost for insertions and deletions
in the middle of an array makes them inefficient for dynamic collections where
elements are frequently added or removed.
### 6. COMMON USE CASES & APPLICATIONS
* As the underlying building block for other data structures like Stacks, Queues,
Heaps, and Hash Tables.
* Storing lookup tables or fixed-size datasets (e.g., a table of month names).
* In image processing, where a 2D array can represent the pixels of an image.
* Matrix operations in scientific computing.
* Sorting algorithms often operate directly on arrays.