Here the one of the easy and efficient method of searching is binary search.
happy coding😋😀
binary search want precondition to all the data must be in sorting.
okay then we start the program of binary search in easy method.
- binary search
#include<stdio.h>
int search(int a[], int l, int r, int val)
{
if (r >= l)
{
int mid = l + (r/ 2);
if (a[mid] == val)
return mid;
else if (a[mid] > val)
return search(a, l, mid - 1, val);
else
return search(a, mid + 1, r, val);
}
else
return 0;
}
main()
{
int val,n,a[10],m;
printf("\nEnter number of elements:");
scanf("%d",&n);
printf("\nEnter array in sorted manner.");
for(int i=0;i<n;i++)
{
printf("\nvalue at a[%d]=",i);
scanf("%d",&a[i]);
}
printf("\nEnter value to search:");
scanf("%d",&val);
m=search(a,0,n-1,val);
if(m!=0)
{
printf("\n%d found at a[%d]",val,m);
}
else
printf("\nElement not found");
}
|
happy coding😋😀

Post a Comment
Hii