অ্যারে ম্যানিপুলেশন (ইনসার্ট, ডিলিট, সার্চ)

অ্যারে (Arrays in C) - সি দিয়ে ডেটা স্ট্রাকচার (DSA using C) - Computer Programming

387

অ্যারে ম্যানিপুলেশন হল একটি গুরুত্বপূর্ণ প্রক্রিয়া যা অ্যারের উপাদানগুলির মধ্যে ইনসার্ট, ডিলিট এবং সার্চ করার জন্য ব্যবহৃত হয়। নিচে C প্রোগ্রামিং ভাষায় এই তিনটি কার্যকলাপের বিস্তারিত আলোচনা করা হলো।


১. ইনসার্ট (Insert)

অ্যারে ইনসার্ট করার মাধ্যমে নতুন উপাদান অ্যারেতে যুক্ত করা হয়। একটি নির্দিষ্ট পজিশনে উপাদান যুক্ত করতে পারা যায়।

উদাহরণ:

#include <stdio.h>

void insert(int arr[], int *size, int element, int position) {
    // Shift elements to the right
    for (int i = *size; i > position; i--) {
        arr[i] = arr[i - 1];
    }
    arr[position] = element; // Insert the new element
    (*size)++; // Increase the size
}

int main() {
    int arr[10] = {1, 2, 3, 4, 5};
    int size = 5;

    int element = 10; // Element to insert
    int position = 2; // Position to insert

    insert(arr, &size, element, position);

    // Print updated array
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]); // Output: 1 2 10 3 4 5
    }

    return 0;
}

২. ডিলিট (Delete)

অ্যারে থেকে একটি উপাদান মুছে ফেলা হলে, বাকি উপাদানগুলিকে স্থানান্তর করতে হয়।

উদাহরণ:

#include <stdio.h>

void delete(int arr[], int *size, int position) {
    // Shift elements to the left
    for (int i = position; i < *size - 1; i++) {
        arr[i] = arr[i + 1];
    }
    (*size)--; // Decrease the size
}

int main() {
    int arr[10] = {1, 2, 3, 4, 5};
    int size = 5;

    int position = 2; // Position to delete

    delete(arr, &size, position);

    // Print updated array
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]); // Output: 1 2 4 5
    }

    return 0;
}

৩. সার্চ (Search)

অ্যারে থেকে একটি নির্দিষ্ট উপাদান খুঁজে বের করার জন্য সার্চিং ব্যবহার করা হয়। সাধারণত Linear Search এবং Binary Search ব্যবহৃত হয়।

৩.১ Linear Search

Linear Search প্রতিটি উপাদান পরীক্ষা করে উপাদান খুঁজে বের করে।

#include <stdio.h>

int linearSearch(int arr[], int size, int element) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == element) {
            return i; // Return the index of the element
        }
    }
    return -1; // Element not found
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = 5;
    int element = 3;

    int index = linearSearch(arr, size, element);
    if (index != -1) {
        printf("Element %d found at index %d\n", element, index); // Output: Element 3 found at index 2
    } else {
        printf("Element not found.\n");
    }

    return 0;
}

৩.২ Binary Search

Binary Search শুধুমাত্র সাজানো অ্যারের জন্য কার্যকরী। এটি মধ্যবর্তী উপাদান পরীক্ষা করে উপাদানটি ছোট বা বড় কিনা তা নির্ধারণ করে।

#include <stdio.h>

int binarySearch(int arr[], int size, int element) {
    int left = 0, right = size - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;

        if (arr[mid] == element) {
            return mid; // Element found
        }
        if (arr[mid] < element) {
            left = mid + 1; // Search right half
        } else {
            right = mid - 1; // Search left half
        }
    }
    return -1; // Element not found
}

int main() {
    int arr[] = {1, 2, 3, 4, 5}; // Sorted array
    int size = 5;
    int element = 4;

    int index = binarySearch(arr, size, element);
    if (index != -1) {
        printf("Element %d found at index %d\n", element, index); // Output: Element 4 found at index 3
    } else {
        printf("Element not found.\n");
    }

    return 0;
}
Content added By
Promotion

Are you sure to start over?

Loading...