Created
September 16, 2012 10:37
-
-
Save huadaonan/3731910 to your computer and use it in GitHub Desktop.
binarysearch
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
#include<stdio.h> | |
int binarysearch(int x,int v[],int n) | |
{ | |
int low,high,mid; | |
low=0; | |
high=n-1; | |
while(low <= high){ | |
mid = low + high / 2; | |
if ( x < v[mid]) | |
high = mid - 1; | |
else if ( x > v[mid]) | |
low = mid - 1; | |
else | |
return mid; | |
} | |
return -1; | |
} | |
main(){ | |
int digits[21] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; | |
int result=0; | |
int x=5; | |
result = binarysearch(x,digits,21); | |
printf("the %d position is in %d",x,result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment