0

I've written the following binary search algorithm and it worked on *my* tests. I want to know if there is something I'm missing, as this is the first time I try to write it, so give me a case in which it would fail, and I'll try fixing it.

I know I don't have a sorting algorithm implemented (I will, shortly) and I know it doesn't work if the numbers are too big.

```
#include<stdio.h>
int bsearch(int a[], int length, int n);
int main(void) {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, n, result;
printf("Enter a number: ");
scanf("%d", &n);
result = bsearch(a, 1, n);
if(result < 0) {
printf("Element not in array");
} else {
printf("Element found: %d", a[result]);
}
printf("\n");
return 0;
}
int bsearch(int a[], int length, int n) {
int min = 0, max = length, mid = max / 2;
if(length < 2) {
if(a[0] == n) {
return 0;
}
} else {
return 1;
}
while(min != mid) {
if(n < a[mid]) {
max = mid;
mid = (max + min) / 2;
} else if(n > a[mid]) {
min = mid;
mid = (min + max) / 2;
} else if(n == a[mid]) {
return mid;
}
}
return -1;
}
```