Searching Operations On Array In Data Structure

In C, there are several ways to search for an element in an array. Here are three commonly used methods:

1. Linear Search:

Linear search is a simple searching algorithm that sequentially checks each element of the array until it finds the target element or reaches the end of the array. Here is an example implementation of linear search in C:

```
#include <stdio.h>

int linear_search(int arr[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == target)
            return i;
    }
    return -1; // Target element not found

}

int main() {
    int arr[] = {5, 2, 9, 1, 7, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    int target = 7;
    int index = linear_search(arr, n, target);
    if (index == -1)
        printf("Target element not found");
    else
        printf("Target element found at index %d", index);
    return 0;
}
```

In this implementation, the `linear_search()` function takes an array, its length, and a target element as input and returns the index of the target element in the array. If the target element is not found, it returns -1.


2. Binary Search:

Binary search is a more efficient searching algorithm for sorted arrays. It works by repeatedly dividing the search interval in half until the target element is found or the search interval is empty. Here is an example implementation of binary search in C:

```
#include <stdio.h>

int binary_search(int arr[], int left, int right, int target) {
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target)
            return mid;
        else if (arr[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return -1; // Target element not found
}

int main() {
    int arr[] = {1, 2, 4, 5, 7, 9};
    int n = sizeof(arr)/sizeof(arr[0]);
    int target = 7;
    int index = binary_search(arr, 0, n-1, target);
    if (index == -1)
        printf("Target element not found");
    else
        printf("Target element found at index %d", index);
    return 0;
}
```

In this implementation, the `binary_search()` function takes a sorted array, the left and right endpoints of the search interval, and a target element as input and returns the index of the target element in the array. If the target element is not found, it returns -1


3. Recursive Binary Search:

Recursive binary search is a recursive version of binary search that also works on sorted arrays. Here is an example implementation of recursive binary search in C:

```
#include <stdio.h>
int recursive_binary_search(int arr[], int left, int right, int target) {
    if (left > right)
        return -1; // Target element not found
    int mid = left + (right - left) / 2;
    if (arr[mid] == target)
        return mid;
    else if (arr[mid] < target)
        return recursive_binary_search(arr, mid+1, right, target);
    else
        return recursive_binary_search(arr, left, mid-1, target);
}

int main() {
    int arr[] = {1, 2, 4, 5, 7, 9};
    int


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext