Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- int a[100],mid,beg,end,key,n,i;
- printf("Enter number of elements in the sorted array:\n");
- scanf("%d",&n);
- printf("Elements: \n");
- for(i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- }
- printf("enter the searching element: ");
- scanf("%d",&key);
- beg=1;
- end=n;
- while(beg<=end)
- {
- mid= (beg+end)/2;
- if(a[mid]==key)
- {
- printf("The position of searching element is %d",mid);
- break;
- }
- else if(a[mid]<key)
- {
- beg=mid+1;
- }
- else if(a[mid]>key)
- {
- end=mid-1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement