Merge pull request #255 from shellhub/master

fix bug
This commit is contained in:
Stephen Curry 2019-09-29 23:43:07 +08:00 committed by GitHub
commit 42f6ba10c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,25 +10,23 @@ int binarySearch(int array[], int leng, int searchX)
left = 0; left = 0;
right = leng - 1; right = leng - 1;
for (i = 0; i < leng; i++) while(left <= right)
{ {
pos = (left + right) / 2; pos = left + (right - left) / 2;
if(array[pos] == searchX)
if (array[pos] == searchX) {
return pos; return pos;
}
else if(array[pos] > searchX)
{
right = pos - 1;
}
else else
{ {
if (array[pos] < searchX) left = pos + 1;
left = pos + 1;
else
{
right = pos - 1;
}
} }
} }
return -1; /* not found */
} }