jerry的小窝jarodlau.blog.chinaunix.net
jarodlau
全部博文(156)
2010年(13)
2008年(39)
2007年(104)
aaaaa5aa
sanyujoj
cynthia
浪花小雨
Bsolar
仇一败
乐百事2
eva2021h
million
遗城浅落
分类: C/C++
2007-06-12 00:03:09
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high)/2; if (x < v[mid]) high = mid + 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ }
这个用了2个test,下面使用一个test
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] ; 2ed version */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high)/2; if ( x != v[mid] (x < v[mid]) ? (high = mid + 1) : (low = mid + 1); else /* found match */ return mid; } return -1; /* no match */ }
上一篇:C语言高效编程的的四招技巧
下一篇:心目中的编程高手
登录 注册