- Data Structures & Algorithms
- DSA - Home
- DSA - Introduction
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Characteristics of Algorithms
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Single Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Tries
- DSA - Heap Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Subsequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
Data Structures & Algorithms - Array Data Structure
![]() Share with a Friend |
📌 Array Data Structure
1️⃣ What is an Array?
An array is a fixed-size data structure that stores elements of the same data type in contiguous memory locations. Each element in the array is accessed using an index, starting from 0.
✅ Key Features of Arrays:
- Fixed size (static allocation in some languages like C/C++).
- Fast access (O(1)) using an index.
- Elements stored sequentially in memory.
- Efficient for read operations, but insertion and deletion can be costly.
2️⃣ Types of Arrays
- One-Dimensional Array (1D Array)
- Stores data in a single row or column.
- Example: int arr[5] = {10, 20, 30, 40, 50};
- Two-Dimensional Array (2D Array)
- A matrix-like structure with rows and columns.
- Example: int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
- Multi-Dimensional Array
- Arrays with more than two dimensions.
- Example: int cube[2][2][2] = {{{1,2},{3,4}},{{5,6},{7,8}}};
3️⃣ Basic Operations on Arrays
1️⃣ Declaration & Initialization
int arr[5] = {10, 20, 30, 40, 50};
int arr[] = {10, 20, 30, 40, 50};
int arr[] = {10, 20, 30, 40, 50};
arr = [10, 20, 30, 40, 50]
2️⃣ Accessing Elements
Elements in an array are accessed using indexing.
Example:
printf("%d", arr[2]); // Output: 30
cout << arr[2]; // Output: 30
System.out.println(arr[2]); // Output: 30
print(arr[2]) # Output: 30
3️⃣ Traversing an Array
We use loops to traverse an array and print its elements.
Example:
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
Output
10 20 30 40 50
#include <iostream> using namespace std; int main() { int arr[] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) cout << arr[i] << " "; return 0; }
Output
10 20 30 40 50
class Test { public static void main(String[] args) { int arr[] = {10, 20, 30, 40, 50}; for (int num : arr) System.out.print(num + " "); } }
Output
10 20 30 40 50
arr = [10, 20, 30, 40, 50] for num in arr: print(num, end=" ")
Output
10 20 30 40 50
4️⃣ Insertion in an Array
Inserting an element at a specific position requires shifting elements.
C Language:
C
#include <stdio.h> void insertElement(int arr[], int *n, int element, int pos) { for (int i = *n; i > pos; i--) arr[i] = arr[i - 1]; arr[pos] = element; (*n)++; } int main() { int arr[10] = {10, 20, 30, 40, 50}, n = 5; insertElement(arr, &n, 25, 2); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
✅ Output:
10 20 25 30 40 50
5️⃣ Deletion in an Array
Deleting an element requires shifting elements.
C Language:
C
#include <stdio.h> void deleteElement(int arr[], int *n, int pos) { for (int i = pos; i < (*n) - 1; i++) arr[i] = arr[i + 1]; (*n)--; } int main() { int arr[5] = {10, 20, 30, 40, 50}, n = 5; deleteElement(arr, &n, 2); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
✅ Output:
10 20 40 50
6️⃣ Searching in an Array
Linear Search (O(n))
Python
def linear_search(arr, key): for i in range(len(arr)): if arr[i] == key: return i return -1 arr = [10, 20, 30, 40, 50] print(linear_search(arr, 30)) # Output: 2
✅ Output:
2
Binary Search (O(log n))
Python
def binary_search(arr, key):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == key:
return mid
elif arr[mid] < key:
left = mid + 1
else:
right = mid - 1
return -1
arr = [10, 20, 30, 40, 50]
print(binary_search(arr, 30)) # Output: 2
Python
def binary_search(arr, key): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == key: return mid elif arr[mid] < key: left = mid + 1 else: right = mid - 1 return -1 arr = [10, 20, 30, 40, 50] print(binary_search(arr, 30)) # Output: 2
✅ Output:
2
4️⃣ Time Complexity of Array Operations
| Operation | Best Case | Worst Case |
|---|---|---|
|
Access |
O(1) |
O(1) |
|
Search |
O(1) (if first element) |
O(n) (linear search) |
|
Insert |
O(1) (end) |
O(n) (beginning) |
|
Delete |
O(1) (last element) |
O(n) (beginning) |
5️⃣ Advantages & Disadvantages of Arrays
✅ Advantages:
- Fast access (O(1)) if index is known.
- Efficient for fixed-size data storage.
- Simple implementation.
❌ Disadvantages:
- Fixed size (cannot grow dynamically in C/C++).
- Insertion & deletion are costly (O(n) in worst case).
- Memory wastage (if allocated size is larger than needed).
6️⃣ Real-Life Applications of Arrays
✅ Used in:
✔ Image processing (2D arrays for pixels).
✔ Database indexing.
✔ Scheduling algorithms (CPU scheduling).
✔ Pathfinding algorithms (A* algorithm).
✔ Implementing other data structures (stack, queue, heap).
Conclusion
- Arrays are simple and efficient for data storage and retrieval.
- However, they are static and have costly insertion/deletion operations.
- Linked Lists are often used when dynamic memory is required.
