Created
May 8, 2013 01:47
-
-
Save cuixin/5537608 to your computer and use it in GitHub Desktop.
Binary Search
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 bsearch(int *a, int n, int v) | |
{ | |
int l = 0, r = n - 1, m = 0; | |
while (l <= r) { | |
m = l + ((r - l) >> 1); | |
if (a[m] > v) { | |
r = m - 1; | |
} else if (a[m] < v) { | |
l = m + 1; | |
} else { | |
return m; | |
} | |
} | |
return -1; | |
} | |
int main(int argc, char **argv) | |
{ | |
int data[100]; | |
int i = 0, index = -1; | |
for (;i < 100; i++) { | |
data[i] = i + 10; | |
} | |
index = bsearch(data, 100, 44); | |
printf("Find index = %d value = %d\n", index, data[index]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment