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