πŸš€ 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:

  1. Traversal

  2. Insertion

  3. Deletion

  4. 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

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”