You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
466 B
19 lines
466 B
8 months ago
|
/* binsearch: найти х в v[0] <= v[1] <= … <= v[n-1] */
|
||
|
int main(int x, int v[], int n)
|
||
|
{
|
||
|
int low, high, mid;
|
||
|
low = 0;
|
||
|
high = n - 1 ;
|
||
|
while (low <= high && x != v[mid]) {
|
||
|
mid = (low + high) / 2;
|
||
|
if (x < v[mid])
|
||
|
high = mid - 1;
|
||
|
else
|
||
|
low = mid + 1;
|
||
|
mid = (low + high) / 2; \
|
||
|
}
|
||
|
if ( x == v[mid])
|
||
|
return mid;
|
||
|
else
|
||
|
return -1; /* совпадения нет */
|
||
|
}
|