Professional Documents
Culture Documents
This search method searches for an element by visiting all the elements sequentially until the element is
found or the array finishes. It follows the array traversal method.
Binary Search:
This search method searches for an element by breaking the search space into half each time it finds the
wrong element. This method is limited to a sorted array. The search continues towards either side of the
mid, based on whether the element to be searched is lesser or greater than the mid element of the current
search space.
Binary search:
#include<stdio.h>
int main()
int a[20],i,x,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;i++)
if(a[i]==x)
break;
if(i<n)
else
Output:
#include <stdio.h>
int main()
scanf("%d",&n);
scanf("%d",&array[i]);
scanf("%d", &search);
low = 0;
high = n - 1;
mid = (low+high)/2;
low = mid + 1;
break;
else
high = mid - 1;
mid = (low + high)/2;
Output:
13found at location 2