What is an Array?
An Array is a collection of elements (values or variables), each identified by an index or subscript, stored in contiguous memory locations.All elements in an array are of the same data type (int, float, char, etc.).
Types of Arrays:
One-Dimensional Array:
Multi-Dimensional Array:
Important Points:
Indexing starts at 0 (in C, C++, Java, Python, etc.).
Array size is fixed at the time of declaration.
Can store homogeneous data only (same data type).
Used for storing multiple values under a single name.
Common Operations:
- Traversal – visiting each element.
- Insertion – adding an element.
- Deletion – removing an element.
- Searching – finding an element.
- Sorting – arranging elements (e.g., Bubble Sort).
Why use Arrays?
Saves memory (grouping variables).
Easy to access & manipulate using loops.
Essential for creating complex data structures (like matrices, stacks, queues).
Simple Example in C:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++) {
printf(“%d “, numbers[i]);
}
return 0;
}
1. Single-Dimensional Array (1D Array)
Definition:
A Single-Dimensional Array is a linear data structure consisting of a finite, ordered set of elements of the same data type. Each element in the array is identified by a unique index.
Declaration Syntax:
data_type array_name[size];
Example:
int numbers[5] = {10, 20, 30, 40, 50};
Here, numbers is an integer array capable of holding 5 elements.
Element Access:
Individual elements of the array can be accessed using their index. The index of the first element is always zero.
printf(“%d”, numbers[2]); // Output: 30
2. Multi-Dimensional Array
Definition:
A Multi-Dimensional Array is an array comprising multiple arrays as its elements. It can be visualized as an array of arrays. The most common form is the two-dimensional array (2D array), representing data in rows and columns (tabular format).
A. Two-Dimensional Array (2D Array)
Declaration Syntax:
data_type array_name[row_size][column_size];
Example:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
This declares a 2×3 array (2 rows, 3 columns).
Element Access:
printf(“%d”, matrix[1][2]); // Output: 6
B. Three-Dimensional Array (3D Array)
Declaration Syntax:
data_type array_name[size1][size2][size3];
Example:
int tensor[2][2][2] = {
{ {1, 2}, {3, 4} },
{ {5, 6}, {7, 8} }
};
Element Access:
printf(“%d”, tensor[1][1][0]); // Output: 7
Key Differences Between Single and Multi-Dimensional Arrays
Basis | Single-Dimensional Array | Multi-Dimensional Array |
Structure | Linear (one row) | Tabular (two or more dimensions) |
Declaration Example | int a[5]; | int a[3][3]; |
Storage Format | Elements stored in a single line | Elements stored in rows and columns |
Access Example | a[2]; (Third element) | a[1][2]; (Element at Row 1, Column 2) |
Usage | Simple lists like marks, prices, etc. | Matrices, tables, images, etc. |
Advantages of Arrays:
Efficient way to store multiple values of the same type.
Easy access through indexing.
Reduces complexity of code by grouping similar variables.
Limitations of Arrays:
Fixed size (cannot grow or shrink dynamically).
All elements must be of the same data type.
Insertion and deletion operations are complex compared to dynamic data structures like linked lists.
Sparse Matrices
A sparse matrix is a matrix that contains a large number of zero elements. In other words, if the number of zero elements in a matrix is significantly higher than the number of non-zero elements, the matrix is called sparse.
Example:
In this matrix, out of 9 elements, only 2 are non-zero. Hence, it is a sparse matrix.
Why Sparse Matrix?
Reduces memory usage by avoiding storage of zero elements.
Speeds up computation by operating only on non-zero elements.
Representations of Sparse Matrix
1. Array (Triplet) Representation
A compact way to store sparse matrices using a 2D array where each row stores information about a non-zero element.
Structure:
Each non-zero element is represented by three values:
- Row Index
- Column Index
- Value
Example Sparse Matrix:
Triplet Representation:
Explanation:
First row represents the position (0,2) having value 5.
Second row represents the position (1,1) having value 8.
- Linked List Representation
A linked list structure can also be used to represent sparse matrices efficiently.
Structure:
Each node in the linked list contains:
Row index
Column index
Value
Pointer to the next node
Example Node Structure in C:
struct Node {
int row;
int col;
int value;
struct Node* next;
};
Linked List for Above Example:
[0, 2, 5] -> [1, 1, 8] -> NULL
Comparison of Representations:
Basis | Array (Triplet) Representation | Linked List Representation |
Memory Usage | Requires extra space for array structure | Requires extra space for pointers |
Access Time | Fast (due to indexing) | Slower (requires traversal) |
Insertion/Deletion | Difficult (fixed size array) | Easy (dynamic memory allocation) |
Complexity | Simple to implement | Slightly complex due to pointers |
Advantages of Sparse Representation:
Saves memory when matrix has many zeroes.
Operations can be limited to non-zero elements, improving efficiency.
When to Use Sparse Matrices:
Large datasets with sparse information (e.g., social networks, recommendation systems). Image processing, machine learning applications with sparse data.
Graph representations with many absent edges.