Array in Data Structure
An array is one of the most basic and commonly used data structures
in computer science. It is used to store a collection of elements of the same
data type in a single variable. All elements in an array are stored in
continuous memory locations, which means they are placed next to
each other in the computer’s memory.
Each element in an array is identified by a unique number called an
index. The index usually starts from 0, so the
first element is accessed using index 0, the second using index 1, and so on.
Because of this indexing system, accessing any element in an array is very fast
and efficient.
Arrays make programs easier to understand and manage because they allow multiple
values to be grouped under a single name. For example, instead of creating
separate variables for storing marks of students, an array can store all marks
together in an organized manner.
Arrays are especially useful when the amount of data is fixed or known in advance.
They are widely used in tasks such as storing lists of numbers, handling strings,
managing tables of data, and implementing other data structures like stacks and
queues.
Due to their simplicity, speed, and ease of use, arrays form the foundation of
many algorithms and advanced data structures in programming.
Characteristics of Array
-
Same Data Type Elements:
An array stores only elements of a single data type. For example, an integer
array can store only integer values, while a character array stores only
characters. This ensures uniformity and makes data processing easier and
more predictable.
-
Contiguous Memory Allocation:
All elements of an array are stored in continuous memory locations. This
arrangement allows the computer to calculate the address of any element
quickly using its index, which improves overall performance.
-
Index-Based Access:
Each element in an array is accessed using an index number. Indexing usually
starts from 0, making it easy to retrieve or update any element directly
without searching through other elements.
-
Fast Data Retrieval:
Because arrays allow direct access through indexing, retrieving an element
takes constant time. This makes arrays very efficient when frequent access
to data is required.
-
Fixed Size:
In most programming languages, the size of an array is fixed at the time of
declaration. This means the number of elements cannot be changed during
execution, which helps in efficient memory management but requires careful
planning.
-
Efficient Memory Usage:
Since arrays do not store extra information like pointers, they use memory
efficiently. This makes them suitable for applications where performance
and memory usage are critical.
Types of Array
1. One-Dimensional Array
A one-dimensional array is the simplest and most commonly used
form of array in programming. In this type of array, data elements are stored
in a single straight line, one after another in continuous
memory locations. Each element is accessed using only one index value.
You can think of a one-dimensional array as a row of boxes, where each box
holds a value and is identified by a number called its index. The index usually
starts from 0, so the first element is accessed using index 0,
the second using index 1, and so on.
One-dimensional arrays are widely used to store simple lists of related data.
For example, they can be used to store marks of students in a class, prices of
products in a shop, roll numbers of students, or temperatures recorded over
several days.
This type of array allows very fast access to data because the memory location
of any element can be calculated directly using its index. However, the size of
a one-dimensional array is usually fixed at the time of declaration, so the
number of elements must be decided in advance.
Syntax (C Language):
data_type array_name[size];
Example:
int marks[5] = {65, 78, 90, 82, 74};
Program:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
for(i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2. Two-Dimensional Array
A two-dimensional array is a type of array that stores data in
the form of rows and columns, making it similar to a table,
grid, or matrix. Instead of using a single index, each element in a
two-dimensional array is accessed using two index values:
one for the row and one for the column.
You can imagine a two-dimensional array like a spreadsheet or a classroom
seating chart, where data is arranged neatly in rows and columns. This structure
is very useful when data naturally fits into a tabular format.
Two-dimensional arrays are commonly used in mathematical operations such as
matrix addition, subtraction, and multiplication. They are also widely used
to store and process tabular data like student records, examination marks,
employee salary tables, and game boards.
Just like one-dimensional arrays, the elements of a two-dimensional array are
stored in continuous memory locations, which allows efficient access using
index values. However, the size of rows and columns is usually fixed at the
time of declaration.
Syntax:
data_type array_name[rows][columns];
Example:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Program:
#include <stdio.h>
int main() {
int mat[2][2] = {{1,2},{3,4}};
int i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
3. Multi-Dimensional Array
Multi-dimensional arrays contain more than two dimensions
and are used in advanced applications such as scientific
simulations and 3D graphics.
int data[2][2][2];
Basic Operations on Array
-
Traversal:
Traversal is the process of visiting each element of an array one by one.
This operation is commonly used when displaying array values, performing
calculations on all elements, or copying data from one array to another.
Traversal ensures that every element is accessed in a systematic order.
-
Insertion:
Insertion means adding a new element into the array at a specific position.
Since arrays use fixed memory locations, inserting an element may require
shifting existing elements to create space. Proper insertion helps maintain
the correct order of data.
-
Deletion:
Deletion refers to removing an element from the array. After deletion, the
remaining elements are usually shifted to fill the empty position. This
operation helps remove unwanted or outdated data and keeps the array
organized.
-
Searching:
Searching is the operation of finding whether a particular value exists in
an array. If the element is found, its position is returned. Searching is
essential in many applications such as record lookup and data validation.
-
Sorting:
Sorting involves arranging array elements in a specific order, such as
ascending or descending. Sorted arrays make searching faster and improve
readability of data. Sorting is widely used in reports, rankings, and data
analysis.
Advantages of Array
-
Fast Index-Based Access:
Arrays allow direct access to any element using its index value. This means
elements can be retrieved or updated instantly without searching, making
arrays very efficient for frequent data access.
-
Simple and Easy to Use:
Arrays are easy to understand and implement, even for beginners. Their
straightforward structure makes programs more readable and reduces
complexity during development.
-
Efficient Use of Memory:
Arrays store data in continuous memory locations without extra overhead such
as pointers. This leads to efficient memory utilization and better
performance compared to some other data structures.
-
Ideal for Fixed-Size Data:
When the number of elements is known in advance, arrays provide a perfect
solution. They allow organized storage and quick access without dynamic
memory allocation.
-
Supports Multiple Operations:
Arrays make it easy to perform common operations such as traversal,
searching, sorting, and updating, which are essential in many programs.
-
Foundation for Other Data Structures:
Many advanced data structures like stacks, queues, and matrices are built
using arrays, making them an important concept in computer science.
Disadvantages of Array
-
Fixed Size Limitation:
The size of an array must be defined at the time of declaration and usually
cannot be changed during program execution. If the allocated size is smaller
than required, the array cannot store additional elements. If the size is
larger than needed, unused memory space remains wasted.
-
Costly Insertion and Deletion Operations:
In arrays, elements are stored in continuous memory locations. When a new
element is inserted or an existing one is removed at any position other than
the end, all subsequent elements must be shifted. This shifting process
increases execution time and reduces efficiency.
-
Memory Wastage:
Arrays may lead to memory wastage when the declared size is not fully utilized.
Since memory is allocated in advance, unused elements still occupy space,
which is inefficient in memory-constrained systems.
-
Homogeneous Data Only:
Arrays can store only elements of the same data type. This limitation makes
them unsuitable for storing different types of related data together, such
as a student’s name, roll number, and marks.
-
Lack of Dynamic Behavior:
Arrays do not automatically grow or shrink based on data requirements.
Handling dynamic data using arrays requires additional logic, making the
program more complex.
Real-Life Examples of Array
-
Daily Attendance Record:
An array can be used to store the daily attendance status of students or
employees, where each index represents a specific person and the value
shows presence or absence.
-
Scores in a Sports Tournament:
The scores of players in a match or tournament can be stored in an array,
allowing quick access and comparison of individual performance.
-
Price List of Products:
Arrays are commonly used to store the prices of products in a shop or
supermarket, where each index corresponds to a specific item.
-
Seats in a Cinema Hall:
Seat numbers in a cinema hall can be represented using an array to track
booked and available seats efficiently.
-
Sensor Data Collection:
Arrays are used to store data collected from sensors, such as humidity,
pressure, or light intensity readings, taken at regular intervals.
Conclusion
Arrays are the foundation of many data structures.
A clear understanding of arrays helps learners
grasp advanced concepts such as linked lists,
stacks, queues, and trees more effectively.