Searching

Searching

Today is the Day 5 of my 100-day DSA challenge and I welcome you all to my journey of DSA. As you all know we were going through the concept of recursion used in programming that reduces the workload of any programmer. Continuing with the concept of recursion, I hope you all enjoy this journey alongside me.

Most of you might be familiar with the concept of arrays, so when we talk about arrays, we generally think of a collection of homogeneous datatypes stored at alternate memory locations called data members. To access any member of an array we traverse the array because the array is devised with the concept of indexing. But what if someone asks us to access the element with the help of the value of element or we need to check if that particular element exists within that particular array, that's where the concept of searching comes into existence.

To check or retrieve an element from any data structure where data is stored. We have two types of search operations that help locate any particular element within the asked data structure. The two types of searching are defined as follows: -

  • Linear or Sequential Search = As the name suggests, it helps to locate the required element from the data structure. It's done with the help of an array of array traversal i.e. it jumps from one element to the next element, and checks every element one at a time. It's not important for the data structure to be sorted and the time complexity of linear search is O(n).
#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 -1;
}
int main(){
    int arr[] = {1,3,76,70,56,100,30};
    int size = sizeof(arr)/sizeof(int);
    int element;
    printf("Enter the element you want to search in this array : ");
    scanf("%d", &element);
    int searchIndex = linearSearch(arr, size, element);
    printf("The element %d was found at %d.", element, searchIndex);
    return 0;
}
  • Binary Search = Binary search is a faster approach for searching any element within the given data structure. Though it may be fast as compared to the linear data structure, it comes with its certain limitation that the array must be sorted. The time complexity for binary search is O(log n).

    For Binary search keep dividing the array into two parts unless you find the required element at the middle position of the current array.

    Formula = (Mid + High)/2

#include<stdio.h>
int binarySearch(int arr[], int size, int element){
    int low, mid, high;
    low = 0;
    high = size - 1;
    while (low <=high)
    {
        mid = (low + high)/2;
        if (arr[mid] == element)
        {
            return mid;
        }
        else if (arr[mid] < element)
        {
            low = mid + 1;
        }
        else
        {
            high = mid - 1;
        }      
    }
    return -1;
}
int main(){
    int arr[] = {1,3,7,30,60,70,100};
    int size = sizeof(arr)/sizeof(int);
    int element;
    printf("Enter the element you want to search : ");
    scanf("%d", &element);
    int searchIndex = binarySearch(arr, size, element);
    printf("The element %d was found at index %d", element, searchIndex);
    return 0;
}