this code is for sentilinear search of an array in dsa

PHOTO EMBED

Tue Dec 19 2023 06:57:59 GMT+0000 (Coordinated Universal Time)

Saved by @deba

#include<stdio.h>
int sentilinearsearch(int arr[], int size, int target)
{
    int last = arr[size-1];
    arr[size - 1] = target;
    int i=0;
    while(arr[i] !=target){
        i++;
    }
    arr[size-1] = last;
    if(i < size-1 || arr[size - 1]== target){
        return i;
    }
    else{
        return -1;
    }
}
int main()
{
    int arr[] = {3,5,1,8,2};
    int size = sizeof(arr)/sizeof(arr[0]);
    int target = 8;

    int result = sentilinearsearch(arr , size, target);

    if (result != -1){
        printf("element %d is found at index%d\n", target,result);
    }
    else{
        printf("element %d is not fopund", target);
    }
    return 0 ;
}
content_copyCOPY