Detailed Summary of Arrays in C (DSA)
1. Introduction to Arrays
An array is a collection of elements stored in contiguous memory locations.
It allows multiple values of the same data type to be stored and accessed using a single variable name.
Characteristics:
- Fixed size: Defined at the time of declaration and cannot be changed.
- Homogeneous elements: All elements are of the same data type.
- Random access: Elements can be accessed directly using their index.
Types of Arrays:
- 1D Array: A linear list of elements (e.g., int arr[5]).
- 2D Array: A table-like structure with rows and columns (e.g., int arr[3][4]).
- Multi-dimensional Arrays: Arrays with more than two dimensions (e.g., int arr[3][4][5]).
2. Declaration and Initialization
Declaration specifies the size and type of the array.
Syntax: int arr[5]; // Declares an array of 5 integers.
Initialization assigns values to the array.
Syntax: int arr[5] = {1, 2, 3, 4, 5};
3. Array Operations
Insertion: Adding an element at a specific position. Time complexity: O(n).
Deletion: Removing an element from a specific position. Time complexity: O(n).
Traversal: Accessing and printing all elements sequentially. Time complexity: O(n).
Searching:
- Linear Search: Sequentially checks each element until the target is found. Time complexity: O(n).
- Binary Search: Efficient for sorted arrays; repeatedly divides the array in half. Time complexity: O(log n).
Sorting:
- Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order. Time complexity: O(n²).
- Selection Sort: Selects the smallest element and swaps it with the first unsorted element. Time complexity:
O(n²).
- Merge Sort: Divides the array into halves, sorts each half, and merges them. Time complexity: O(n log n).
4. Applications of Arrays
1. Storing data in programs.
2. Matrix representation using 2D arrays.
3. Dynamic programming for intermediate results in algorithms.
4. Hashing as a base for hash tables.
5. Implementation of other data structures like stacks, queues, and heaps.
5. Sample Programs
Example 1: Traversing an Array:
#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;
Example 2: Linear Search:
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) return i; // Key found
return -1; // Key not found
int main() {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
int index = linearSearch(arr, 5, key);
if (index != -1)
printf("Element found at index %d", index);
else
printf("Element not found");
return 0;
Example 3: Binary Search:
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid; // Key found
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1; // Key not found
int main() {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
int index = binarySearch(arr, 5, key);
if (index != -1)
printf("Element found at index %d", index);
else
printf("Element not found");
return 0;
Key Takeaways
Arrays are simple yet powerful for storing and manipulating data.
They offer fast random access but have limitations, such as fixed size and costly insertions/deletions.
Mastering array operations is essential for understanding advanced data structures like linked lists, trees, and
graphs.