π Array Operations in C – Insertion, Deletion, Searching (DSA Guide)
π‘ Did You Know?
Arrays are simple… but inserting or deleting elements is not.
π That’s where real Data Structures (DSA) logic begins π
π° Introduction
Since you have already learned the basics of arrays in C,
now it’s time to understand how arrays are used in Data Structures (DSA).
π In DSA, we don’t just store data —
π We insert, delete, and search efficiently.
π₯ Types of Array Operations
There are 4 main operations:
Traversal
Insertion
Deletion
Searching
πΉ 1. Traversal
π Traversal means accessing each element one by one
πΌ️ Visualization
π» Example:
#include <stdio.h> // Includes standard input-output library for printf function
int main() { // Main function where program execution starts
int arr[5] = {10, 20, 30, 40, 50};
// Declares an integer array of size 5 and initializes it with values
for(int i = 0; i < 5; i++) {
// Loop starts from index 0 and runs till index 4 (total 5 elements)
printf("%d ", arr[i]);
// Prints each element of the array followed by a space
}
return 0;
// Returns 0 to indicate successful execution of the program
}
π€ Output
10 20 30 40 50
π‘ Real-Life Example
π Checking marks of all students one by one
πΉ 2. Insertion
π Insertion means adding a new element at a specific position
πΌ️ Visualization
π» Example Code
#include <stdio.h> // Include standard I/O library
int main() { // Program execution starts here
int arr[10] = {10, 20, 30, 40}; // Array of size 10 with 4 initial elements
int n = 4, pos = 2, value = 25; // n = current size, pos = insert position, value = element to insert
for(int i = n; i > pos; i--) { // Loop to shift elements right from position
arr[i] = arr[i-1]; // Shift each element one position ahead
}
arr[pos] = value; // Insert value at desired position
n++; // Increase array size after insertion
for(int i = 0; i < n; i++) { // Loop to print updated array
printf("%d ", arr[i]); // Print each element
}
return 0; // End program successfully
}
π€ Output
10 20 25 30 40
⚡ Time Complexity
π O(n)
π‘ Real-Life Example
π Adding a new student record in a list
πΉ 3. Deletion
π Deletion means removing an element from array
πΌ️ Visualization
π» Example Code
#include <stdio.h> // Include standard input-output library
int main() { // Main function starts
int arr[5] = {10, 20, 30, 40}; // Array with initial 4 elements (size 5)
int n = 4, pos = 1; // n = number of elements, pos = position to delete
for(int i = pos; i < n-1; i++) { // Loop to shift elements left after deletion
arr[i] = arr[i+1]; // Replace current element with next element
}
n--; // Decrease size after deletion
for(int i = 0; i < n; i++) { // Loop to print updated array
printf("%d ", arr[i]); // Print each element
}
return 0; // End of program
}
π€ Output
10 30 40
⚡ Time Complexity
π O(n)
π‘ Real-Life Example
π Removing a student who left the class
πΉ 4. Searching
π Searching means finding an element in array
π» Linear Search Example
#include <stdio.h> // Include standard input-output library
int main() { // Main function starts
int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize array
int key = 30, found = 0; // key = element to search, found = flag (0 = not found)
for(int i = 0; i < 5; i++) { // Loop through each element of array
if(arr[i] == key) { // Check if current element matches key
printf("Element found at index %d", i); // Print index if found
found = 1; // Set flag to indicate element is found
break; // Exit loop after finding element
}
}
if(found == 0) { // Check if element was not found
printf("Element not found"); // Print message if not found
}
return 0; // End of program
}
π€ Output
Element found at index 2
⚡ Time Complexity
π O(n)
π‘ Real-Life Example
π Searching a contact in your phone
π Summary Table
⚠️ Common Mistakes
❌ Forgetting array size limit
❌ Wrong index position
❌ Not shifting elements properly
π‘ Pro Tip
π Arrays are simple, but operations can be costly (O(n)).
π That’s why advanced structures (Linked List, etc.) exist.
π§ͺ Practice Questions (with Answers)
1. What is traversal?
π Answer:
Accessing each element of array one by one.
2. What is insertion?
π Answer:
Adding a new element at a specific position.
3. What is deletion?
π Answer:
Removing an element from array.
4. What is searching?
π Answer:
Finding an element in array.
π Next Topic
π Searching Algorithms (Linear Search vs Binary Search)
π Final Words
Array operations are the foundation of problem solving in DSA.
π Master these:
You will understand all future topics easily
Your logic will become strong
π Keep practicing and keep growing!
π’ Support & Share
π¬ If you found this helpful, share it with your friends!
π Follow Learning Growth Hub for more powerful DSA content.
π Keep Learning. Keep Coding. Keep Growing.
✨ Written by Krishna Popat
π± Founder – Learning Growth Hub
Comments
Post a Comment