Array Searching
Array Searching:
We use array searching to find which of any array element have value equals to a given target value. There are two techniques for array searching:linear(or sequential search)
Binary Search.
In Binary search,each array element value is compared to the target value one by one from first index to last index until we find the matched value ,Useful for small and unsorted arrays.Binary search
can be used of the array if not found one half of the array is searched in the similar way and one half is skipped. We Continue the same process until we find the matched value.
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],n,i,target;
printf("How many number?:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d value:",i+1);
scanf("%d",&a[i]);
}
printf("enter number to search:");
scanf("%d",&target);
for(i=0;i<n;i++)
{
if(a[i]==target)
printf("Found at %d",i+1);
}
if(a[i]==n)
printf("not found");
getch();
}
Output
Click here : How to use array Function ?
Post a Comment