user-0 提问: C语言中的2分法是什么意思 怎么弄 例如这题

回复:
user: user-1
content: 先计算出数组中元素个数n，再取出第n/2（数组中间的那个数）与要查找的数比较。如果比要查找的小，那么取出第n/2+1 ---n中间的数再比较。

user: user-2
content: 先 http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’，再点立即安装  再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\bsearch.c /*** *bsearch.c - do a binary search * *   Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *   defines bsearch() - do a binary search an an array * *******************************************************************************/  #include <cruntime.h> #include <stdlib.h> #include <search.h> #include <internal.h>  #if defined (_M_CEE) #define __fileDECL  __clrcall #else  /* defined (_M_CEE) */ #define __fileDECL  __cdecl #endif  /* defined (_M_CEE) */ /*** *char *bsearch() - do a binary search on an array * *Purpose: *   Does a binary search of a sorted array for a key. * *Entry: *   const char *key    - key to search for *   const char *base   - base of sorted array to search *   unsigned int num   - number of elements in array *   unsigned int width - number of bytes per element *   int (*compare)()   - pointer to function that compares two array *           elements, returning neg when #1 < #2, pos when #1 > #2, and *           0 when they are equal. Function is passed pointers to two *           array elements. * *Exit: *   if key is found: *           returns pointer to occurrence of key in array *   if key is not found: *           returns NULL * *Exceptions: *   Input parameters are validated. Refer to the validation section of the function. * *******************************************************************************/  #ifdef __USE_CONTEXT #define __COMPARE(context, p1, p2) (*compare)(context, p1, p2) #else  /* __USE_CONTEXT */ #define __COMPARE(context, p1, p2) (*compare)(p1, p2) #endif  /* __USE_CONTEXT */  #if !defined (_M_CEE) _CRTIMP #endif  /* !defined (_M_CEE) */  SECURITYSAFECRITICAL_ATTRIBUTE #ifdef __USE_CONTEXT void * __fileDECL bsearch_s (     REG4 const void *key,     const void *base,     size_t num,     size_t width,     int (__fileDECL *compare)(void *, const void *, const void *),     void *context     ) #else  /* __USE_CONTEXT */ void * __fileDECL bsearch (     REG4 const void *key,     const void *base,     size_t num,     size_t width,     int (__fileDECL *compare)(const void *, const void *)     ) #endif  /* __USE_CONTEXT */ {     REG1 char *lo = (char *)base;     REG2 char *hi = (char *)base + (num - 1) * width;     REG3 char *mid;     size_t half;     int result;      /* validation section */     _VALIDATE_RETURN(base != NULL || num == 0, EINVAL, NULL);     _VALIDATE_RETURN(width > 0, EINVAL, NULL);     _VALIDATE_RETURN(compare != NULL, EINVAL, NULL);          /*         We allow a NULL key here because it breaks some older code and because we do not dereference         this ourselves so we can't be sure that it's a problem for the comparison function         */      while (lo <= hi)     {         if ((half = num / 2) != 0)         {             mid = lo + (num & 1 ? half : (half - 1)) * width;             if (!(result = __COMPARE(context, key, mid)))                 return(mid);             else if (result < 0)             {                 hi = mid - width;                 num = num & 1 ? half : half-1;             }             else             {                 lo = mid + width;                 num = half;             }         }         else if (num)             return (__COMPARE(context, key, lo) ? NULL : lo);         else             break;     }      return NULL; }  #undef __fileDECL #undef __COMPARE

user: user-3
content: 你去看看数据结构吧，二分查找法是属于数据结构的内容，里面有详细的解释

user: user-0
content: 能教我怎么做

user: user-4

user: user-5
content: 思路如下： 排序后，因为升序排列，所以元素值大的对应的下标大，即下标从0~19，而元素值也依次增大，可以先选取【0】跟【19】对应的元素值，Avr = (a[0]+a[19])/2，如果当前要比较的数值Cur_data < Avr，那么说明Cur_data在下标【0】~【19】之间，因此，继续比较，取【0】跟【10】，如果Avr < (a[0] + a[10])/2 ，说明Avr在这个区间之内....依此下去，就会找到Cur_data对应的位置

user: user-6
content: For reference only #include<stdio.h>  /*选择排序*/ void select_sort(int *arry, int len) {     int i,j,tmp;     for(i = 0; i < len; ++i)         for(j = i+1; j < len; ++j)         {             if( arry[j] < arry[i])             {                 tmp = arry[i];                 arry[i] = arry[j];                 arry[j] = tmp;             }         } }  /*二分查找*/ int binary_search(int *arry, int len, int key) {     int low = 0,high = len - 1 ;     int mid = (low + high) / 2;      while(low < high)     {         if(arry[mid] == key)             return mid;         else if(arry[mid] < key)         {             low = mid + 1 ;             mid = (low + high) / 2;         }         else          {             high = mid - 1;             mid = (low + high) / 2;         }     }     /*数组中没有key*/     return -1; }  int main() {     int test[20];     int i;     printf("输入:");     for(i = 0; i < 20; ++i)     {         scanf("%d",&test[i]);     }      /*选择排序*/     select_sort(test,20);      printf("输出:");     for(i = 0; i < 20; ++i)     {         printf("%d ",test[i]);     }      while(1)     {         char ch;         int n;         int key;         printf("输入:");         scanf("%d",&key);         n = binary_search(test,20,key);         if(n >= 0)         {             printf("输出:%d\n",n);             break;         }         else         {             printf("输出: 无此数\n");         }     } }

user: user-6
content: 不知道楼主还记不记得,高中时候 的二分法求解方程近似解.

user: user-7
content: void InsertSort(int a[], int n)   {       for(int i= 1; i<n; i++){           if(a[i] < a[i-1]){               //若第i个元素大于i-1元素，直接插入。小于的话，移动有序表后插入               int j= i-1;                int x = a[i];        //复制为哨兵，即存储待排序元素               a[i] = a[i-1];           //先后移一个元素               while(x < a[j]){  //查找在有序表的插入位置                   a[j+1] = a[j];                   j--;         //元素后移               }               a[j+1] = x;      //插入到正确位置           }     }   	 }     int binSearch(const int*Array,int start,int end,int key){ 	int left,right; 	int mid; 	left=start; 	right=end; 	 	while(left<=right){ 		mid=(left+right)/2; 		if(key==Array[mid]){ 			return mid; 		} 		else if(key<Array[mid]){ 			right=mid-1; 		} 		else if(key>Array[mid]){ 			left=mid+1; 		} 	} 	return -1; }  int main(){       int arr[20] = {100, 10, -125, -9, 0, 90, 70, 60, 300, -250, -72, 39, 48, 22, 83, 159, 142, -129, -24, 539};       InsertSort(arr,20); 	int idx=binSearch(arr,0,20,-129); 	if(idx!=-1) 		printf("%d\n",idx); 	idx=binSearch(arr,0,20,301); 	if(idx!=-1) 		printf("%d\n",idx); 	else 		printf("无此数\n"); 	return 0; }