write a c program for sentinal search using array data structure

 

write a c program for sentinal search using array  data structure

                                

                                                                        




#include<stdio.h>

int compcount=0;
int sentinallinearsearch(int A[],int nint key){
        int i=0;
        A[n]=key/* add sentinal element at the end */
        while(A[i]!= key)

        {
            compcount ++;
            i++;

        }
        compcount ++;
        if (i==n)
        {
        return -1;   /* sentinal reached , key nit found*/
        }
        else
        {
            return i/* return postion where key is found */
        }

        
        }
void accept (int A[],int n){

int i;
printf("Enter the %d element ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]); 
}

void main(){

int A[20],n,key,pos;
printf("How many numbers:-");
scanf("%d",&n);
accept(A,n);
printf("Enter the key to be searched :-");
scanf("%d",& key);

pos=sentinallinearsearch(A,n,key);
if (pos==-1)

printf("\n %d not foound in the array",key);
else
printf("\n %d found at position %d",key,pos);
printf("\n the total number of comparisons = %d \n ",compcount);

}   







Post a Comment

0 Comments