This commit is contained in:
shellhub 2019-08-25 21:16:42 +08:00
parent f14320d137
commit 7f9d115226
1 changed files with 11 additions and 13 deletions

View File

@ -10,25 +10,23 @@ int binarySearch(int array[], int leng, int searchX)
left = 0;
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)
return pos;
else
{
if (array[pos] < searchX)
left = pos + 1;
else
return pos;
}
else if(array[pos] > searchX)
{
right = pos - 1;
}
else
{
left = pos + 1;
}
}
return -1; /* not found */
}