Array
What is an Array?
An array is a fixed-size data structure that consists of a collection of elements of same type, each identified by an index. Arrays are one of the simplest and most widely used data structures in programming. They store elements of the same type in contiguous memory locations, allowing efficient access to any element using its index. The only downside is that the memory location has to be reserved at the time of creation and cannot be changed dynamically.
Key Characteristics
- Fixed Size
- Homogeneous Elements
- Contiguous Memory Allocation
- Indexed Access
Types of Arrays
-
One-Dimensional Array: A linear array where elements are accessed using a single index.
- Example:
int arr[5] = {1, 2, 3, 4, 5};
- Example:
-
Multi-Dimensional Array: Arrays with more than one dimension, such as two-dimensional arrays (matrices).
- Example:
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
- Example:
-
Dynamic Arrays: Arrays that can change size dynamically during runtime, such as
vectors
in C++ orArrayList
in Java.
Operations on Arrays
-
Accessing Elements
- Elements are accessed using their index.
- Example:
arr[2]
accesses the third element in the arrayarr
.
-
Insertion
- Adding an element at a specific index requires shifting elements to make space.
- Example: To insert an element at index 2, shift elements from index 2 onwards and then place the new element.
-
Deletion
- Removing an element requires shifting elements to fill the gap.
- Example: To delete an element at index 2, shift elements from index 3 onwards one position to the left.
-
Traversal
- Visiting each element of the array for processing.
- Example: Using a loop to print all elements of the array.
-
Searching
- Finding the index of an element in the array.
- Linear Search: O(n) time complexity.
- Binary Search: O(log n) time complexity (requires sorted array).
-
Sorting
- Arranging elements in a specific order (ascending or descending).
- Example: Using sorting algorithms like Quick Sort, Merge Sort, or Bubble Sort.
Advantages of Arrays
- Constant-Time Access: Elements can be accessed in O(1) time using their index.
- Memory Efficiency: Arrays have a low memory overhead compared to other data structures like linked lists.
- Cache-Friendly: Contiguous memory allocation makes arrays more cache-friendly, leading to better performance in terms of memory access speed.
Disadvantages of Arrays
- Fixed Size: The size of the array is fixed and cannot be changed after its creation, leading to potential wastage of memory or insufficient space.
- Inefficient Insertion/Deletion: Inserting or deleting elements, especially in the middle, can be inefficient as it requires shifting elements.
- Homogeneous Data: Arrays can only store elements of the same type, limiting flexibility.
Applications of Arrays
- Storing and Accessing Data: Arrays are used to store data that needs to be accessed quickly using indices.
- Implementation of Other Data Structures: Arrays are the foundation for implementing other data structures like stacks, queues, and hash tables.
- Mathematical Computations: Arrays are used in scientific computations and matrix operations.
- Graphics and Image Processing: Arrays are used to store pixel values and manipulate images.