急 求一个编程

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:43:13
The function written below is a binary search one which decides if a particular value x occurs in the sorted array v. The elements of v are in increasing order. The function returns the position(a number between 0 and n-1) if x occurs in v, and –1 if not. To complete the program, Please.

int binasearch(int x, int v[], int n)
{ int low, high, mid;
low=0;
拜托拜托

int binasearch(int x, int v[], int n)
{ int low, high, mid;
low=0;
high=n-1;
while(1){
mid=n/2;
if(x==v[mid])return mid;
if(low==mid)return -1;
if(x>v[mid])low=mid;
else high=mid;
}
}

#include <stdio.h>

/*
二分法检索
通过调用辅助函数binarySearchHelp(int x, int v[], int from ,int to)
来完成具体的检索功能
*/
int binarySearch(int x, int v[], int n);

/*辅助函数,该函数具体实现二分法检索*/
int binarySearchHelp(int x, int v[], int from, int to);

int main()
{
int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("the index of 5 is : %d\n", binarySearch(5, v, sizeof(v) / sizeof(v[0])));
printf("the index of 0 is : %d\n", binarySearch(0, v, sizeof(v) / sizeof(v[0])));

return 0;
}

int binarySearch(int x, int v[], int n)
{
return binarySearchHelp(x, v, 0, n - 1);
}

int binarySearchHel