Array in Data Structures
• An array is a collection of elements of the
same data type stored in contiguous memory
locations.
• It allows easy access to data using index
numbers.
Definition
• An array is a data structure that stores a fixed-
size sequential collection of elements of the
same type.
• Example (in C):
• int arr[5] = {10, 20, 30, 40, 50};
Characteristics of Arrays
• - Fixed size
• - Same data type
• - Contiguous memory allocation
• - Direct access using index
Types of Arrays
• 1. One-Dimensional Array (1D)
• Example: int marks[5] = {80, 70, 90, 85, 75};
• 2. Two-Dimensional Array (2D)
• Example:
• int matrix[2][3] = {
• {1, 2, 3},
• {4, 5, 6}
• };
Operations on Arrays
• Traversal - Accessing each element
• Insertion - Adding a new element
• Deletion - Removing an element
• Searching - Finding an element
• Sorting - Arranging elements in order
Representation in Memory
• Arrays are stored in contiguous memory
blocks.
• Address(arr[i]) = Base Address + (i * Size of
Data Type)
Advantages and Disadvantages
• Advantages:
• - Easy access using index
• - Efficient memory usage
• Disadvantages:
• - Fixed size
• - Insertion and deletion are time-consuming
Applications
• - Storing data lists
• - Matrices and tables
• - Algorithms (sorting, searching)
• - Foundation for stacks, queues, etc.
Example Program in C
• #include <stdio.h>
• int main() {
• int arr[5] = {10, 20, 30, 40, 50};
• for (int i = 0; i < 5; i++) {
• printf("%d ", arr[i]);
• }
• return 0;
• }
Conclusion
• Arrays are a fundamental concept in data
structures that allow efficient storage and
access of elements.
• They form the foundation for more complex
structures like stacks, queues, and matrices.