Array
Array
ARRAY – Short Notes
🔹 Definition
An array is a collection of similar data types stored in contiguous memory locations and accessed using an index.
Example:int arr[5] = {10, 20, 30, 40, 50};
🔹 Key Characteristics
Stores homogeneous data (same data type)
Fixed size (size must be defined at creation)
Elements are stored in continuous memory
Indexing usually starts from 0
Fast access using index (O(1))
🔹 Types of Arrays
1. One-Dimensional Array (1D)
Stores elements in a single row.
Example:
int arr[5] = {1, 2, 3, 4, 5};2. Two-Dimensional Array (2D)
Used to store data in rows and columns (matrix form).
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};3. Multi-Dimensional Array
Arrays with more than two dimensions (rarely used).
🔹 Declaration & Initialization
Declaration
datatype arrayName[size];Initialization
int a[3] = {10, 20, 30};🔹 Accessing Array Elements
Using index:
a[0] = 10;
printf("%d", a[1]);🔹 Memory Representation
If base address =
BSize of each element =
wAddress of
arr[i]=
B + (i × w)
🔹 Common Array Operations
Operation | Description | Time Complexity |
|---|---|---|
Traversal | Access all elements | O(n) |
Insertion | Add element | O(n) |
Deletion | Remove element | O(n) |
Searching (Linear) | Search sequentially | O(n) |
Searching (Binary) | On sorted array | O(log n) |
Updating | Modify element | O(1) |
🔹 Advantages
✅ Fast access
✅ Easy to use
✅ Efficient for storing fixed-size data
🔹 Disadvantages
❌ Fixed size
❌ Memory wastage possible
❌ Insertion & deletion are costly
❌ Cannot store different data types
🔹 Applications of Arrays
Searching & Sorting algorithms
Matrix operations
Stack & Queue implementation
Graphs and Dynamic Programming
Storing large datasets