Created
February 18, 2016 08:17
-
-
Save 78526Nasir/06f963d50b39388f3bee to your computer and use it in GitHub Desktop.
Binary Search Implementation in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Binary Search /// | |
#include<stdio.h> | |
main() | |
{ | |
int a[]={1,2,3,4,5,6,7,8,9,10},i,start,end,mid,item; | |
printf("Enter a item to Search:"); | |
scanf("%d",&item); | |
start=0; | |
end=9; | |
printf("\n"); | |
while(start<=end) | |
{ | |
mid=(start+end)/2; | |
printf("start=%d \tend=%d \ta[mid]=%d \titem=%d\n",start,end,a[mid],item); | |
if(a[mid]==item) | |
{ | |
printf("\nItem Found at position %d\n",mid+1); | |
break; | |
} | |
else if(a[mid]>item) | |
end=mid-1; | |
else | |
start=mid+1; | |
} | |
if(start>end) | |
{ | |
printf("start=%d \tend=%d \ta[mid]=%d \titem=%d\n",start,end,a[mid],item); | |
printf("\nItem Not found !!\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment