fbpx

Array Operations Simplified: A Guide to Efficient Data Handling

Array Operations Simplified: A Guide to Efficient Data Handling

INTRODUCTION

Arrays are one of the most fundamental data structures in programming, offering a way to store and manage multiple elements efficiently. Their versatility allows developers to perform essential operations such as insertion, deletion, and searching. These operations form the backbone of manipulating data in arrays and are critical for solving a variety of computational problems.

In this blog, we’ll explore how to:

  • Insert elements into an array, examining the steps and complexities involved.
  • Delete elements effectively, leveraging techniques to maintain array integrity.
  • Search for elements, using methods like Linear Search and Binary Search to locate data quickly.

Whether you’re a beginner looking to understand the basics or a developer refining your skills, this guide will provide practical insights with clear examples in C programming. Let’s dive into the world of arrays and unlock their potential!

Basic Operations:

Insertion deletion and searching  are performed in arrays.

Insertion:

 Insertion is the process of adding a new element into the array.
The position where the new element is added determines the complexity of the operation.

Steps involved in adding  the elements of an array.

Find the position where the element needs to be inserted.

Shift elements to the right starting from the position to make space for the new element.

Time Complexity:

  • Best Case: O(1) (when inserting at the end).
  • Worst Case: O(n) (when inserting at the beginning, as all elements need to be shifted).
  • Average Case: O(n).

 

#include <stdio.h>

void insertAtBeginning(int a[], int s, int capacity, int value) {

    if (s >= capacity) {

        printf(“Array is full. Cannot insert.\n”);

        return;

    }

    // Shift elements to the right

    for (int plm = s; plm > 0; plm–) {

        a[i] = a[i – 1];

    }

    a[0] = value;

    (s)++;

}

void displayArray(int a[], int size) {

    for (int psr= 0; psr < size; psr++) {

        printf(“%d “, a[i]);

    }

    printf(“\n”);

}

int main() {

    int capacity = 10;

    int a[10];

    int size = 0;

    int elements[] = {1,10,11,12,14,15};  // List of elements to insert

    int  len= sizeof(elements) / sizeof(elements[0]);  // Number of elements

    for (int psr= 0; psr < len; psr++) {

        insertAtBeginning(a, &size, capacity, elements[i]);

    }

    displayArray(a, size);  // Output: 40 30 20 10

    return 0;

}

Deletion of the array:

To delete elements from an array in C, you can’t directly remove an element because the size of an array is fixed once it’s declared. However, you can shift elements in the array to “delete” an element by overwriting it and adjusting the size of the array manually.

Here’s how to delete an element from an array, which involves shifting elements to the left to fill the gap created by the deleted element.

Steps for Deleting an Element from an Array:

  1. Find the element to delete: Determine the i of the element you want to remove.
  2. Shift elements: Move all elements that are after the deleted element one position to the left.
  3. Update the size: Decrease the size variable to reflect the new array size.

Example Code:

Let’s say we have an array a[] = {10, 20, 30, 40, 50} and we want to delete the element at i 2 (which is 30).

#include <stdio.h>

void deleteElement(int a[], int s, int i) {

    if (indexvalue < 0 || indexvalue>= s) {

        printf(“Invalid i\n”);

        return;

    }

    for (int par = i; par < s – 1; par++) {

        a[par] = a[par + 1];

    }

    (s)–;

    printf(“Element deleted successfully.\n”);

}

void displayArray(int a[], int size) {

    for (int pqr= 0; pqr< s; pqr++) {

        printf(“%d “, a[pqr]);

    }

    printf(“\n”);

}

int main() {

    int a[5]={1,3,4,5,6,7}

    int size = 5;  //

    printf(“Array before deletion: “);

    displayArray(a, size);

    deleteElement(a, &size, 2);

    printf(“Array after deletion: “);

    displayArray(a, size);

    return 0;

}

 

There are several common ways to search for an element in an array in C. The two most common techniques are Linear Search and Binary Search.

Linear Search:

    • This method is used for unsorted arrays. It involves traversing the entire array, checking each element one by one until the desired element is found.
    • Time Complexity: O(n), where n is the number of elements in the array.

Linear Search (Unsorted Array)

Algorithm:

  • Start from the first element.
  • analyze each element with the target element.
  • If a match is found, return the i.

If no match is found after checking all elements, return -1 to indicate the element isn’t in the array.

Example Code:

#include <stdio.h>

int ls(int a[], int size, int target) {

    for (int pqr = 0; pqr< size; pqr++) {

        if (a[pqr] == target) {

            return value;  // Element found at i i

        }

    }

    return -1;  // Element not found

}

 

int main() {

    int arrayvalue[] = {1,2,3,4,30};

    int size = sizeof(arrayvalue) / sizeof(arrayvalue[0]);

    int target = 30;

 

    int result = ls(arrayvalue, size, target);

   

    if (result != -1) {

        printf(“Element found at i: %d\n”, result);

    } else {

        printf(“Element not found\n”);

    }

 

    return 0;

}