全部博文(7)
2008年(7)
分类: C/C++
2008-03-25 08:34:38
int binsearch(int x, int v[], int n)
{ int low = 0; int high = n - 1; int mid; while (low <= high) {
mid = (low + high) / 2; if (x > v[mid]) low = mid + 1; else if (x < v[mid]) high = mid - 1; else return mid; /* found match, 返回其位置 */ } return -1; /* no match 返回-1 */ } |
/* 得到一个数量为n的不重复的随机数组 */ |