double b_search(double up, double down) { double mid = (up + down) / 2; double cmp; if(fabs(up - down) < 0.000000001) return up; cmp = func(mid); if(cmp == 0) return mid; if(cmp > 0) return b_search(mid, down); return b_search(up, mid); }
//{{{int i_b_search(int key, int *D, int D_size, int *I, int I_size, int *r) int i_b_search(int key, int *D, int D_size, int *I, int I_size) { int hi, lo; int region = i_search(key, I, I_size + 1, D_size, &hi, &lo); //printf("i:%d,%d,%d\n",region,lo,hi); return b_search(key, D, D_size, lo, hi); }
long int optimal_cut(int strt,int end) { long int pos,cost,left_cost,right_cost,i,temp_cost; if(p[redir[strt]][redir[end]]>=0) return p[redir[strt]][redir[end]]; pos=b_search(c,0,n+1,strt); if(c[pos+1]==end) { p[redir[strt]][redir[end]]=0; return 0; } cost=99999999; for(i=pos+1; c[i]<end; i++) { left_cost=optimal_cut(strt,c[i]); right_cost=optimal_cut(c[i],end); p[redir[strt]][redir[c[i]]]=left_cost; p[redir[c[i]]][redir[end]]=right_cost; temp_cost=end-strt+left_cost+right_cost; if(temp_cost<cost) cost=temp_cost; } p[redir[strt]][redir[end]]=cost; return cost; }
/* メイン */ int main(void) { int i, target, work[N], index; clock_t start,finish; for( i = 0; i < N; i++ ) work[i] = rand() % N; start=clock(); #if LINEAR #else quick(work,0,N-1); #endif for( target = N/2-5000; target < N/2+5000; target++ ) { #if LINEAR index = l_search(work,target); #else index = b_search(work,0,N-1,target); #endif printf("TARGET=%d: ", target); if ( index != -1 ) printf("○\n"); else printf("×\n"); } finish=clock(); printf("%d\n",finish-start); return 0; }
// 二分查找的递归实现 void example1() { int arr[10] = {15, 17, 20, 23, 25, 26, 27, 53, 63, 99}; int key = 20; int rs = b_search(arr, 10, key); printf("%s\n", rs == 0 ? "NO" : "YES"); }
int main() { int n; printf("Enter the number of elements of the array \n"); scanf("%d",&n); int array[n]; int i =0; for(i = 0; i < n; i++) { printf("Eneter element %d of the array \n",i); scanf("%d",&array[i]); } sort(array,n); display(array, n); int search; printf("Enter the element that u want to search: \n"); scanf("%d",&search); int ele = b_search(array , n ,search); printf("the return value of the search is %d: \n",ele); }
//{{{int t_b_search(int key, int *D, int D_size, int *T, int T_size) int t_b_search(int key, int *D, int D_size, int *T, int T_size) { int hi, lo; int region = t_search(key, T, T_size, D_size, &hi, &lo); //printf("t:%d,%d,%d\n",region,lo,hi); int r = b_search(key, D, D_size, lo, hi); return r; }
int countGreaterNumbers(struct transaction *Arr, int len, char *date) { if (len <= 0) return -1; int res; res = b_search(Arr, 0, len - 1, date, len); return res; }
/* 二分探索 */ int b_search(int data[], int low, int high, int target) { int mid; #if LOOP /* ループバージョン */ /* 実装せよ */ /* low <= high の間はループ(low > highなら見つからなかった) */ /* mid に low と high の中間値をセット */ /* data[mid] より target が小さければ high を mid-1 に */ /* data[mid] より target が大きければ low を mid+1 に */ /* data[mid] == target だったらループを抜ける */ /* 見つかったなら mid を返す */ /* 見つからなかったなら -1 を返す */ while(low<=high){ mid=(low+high)/2; if(data[mid]==target){ return mid; }else if(data[mid]<target){ low=mid+1; }else{ high=mid-1; } } return -1; #else /* 再帰バージョン */ /* 実装せよ */ /* low > high なら見つからなかったということで -1 を返す */ /* mid に low と high の中間値をセット */ /* data[mid] より target が小さければ前半部分で再帰 */ /* data[mid] より target が大きければ後半部分で再帰 */ /* data[mid] == target だったら見つかったということで mid を返す */ if(low>high) return -1; mid=(low+high)/2; if(data[mid]>target){ return b_search(data,low,mid-1,target); }else if(data[mid]<target){ return b_search(data,mid+1,high,target); }else{ return mid; } #endif }
int i_b_search(int key, int *D, int D_size, int *I, int I_size) { int b = b_search(key, I, I_size, -1, I_size); int lo = -1, hi = D_size; int new_hi = ( (b+1)*hi + (I_size - (b+1))*lo ) / I_size; int new_lo = ( (b)*hi + (I_size - (b+1))*lo ) / I_size; if (b == 0) new_lo = -1; else if (b == I_size) { new_hi = D_size; new_lo = ( (b-1)*hi + (I_size - (b+1))*lo ) / I_size; } return b_search(key, D, D_size, new_lo, new_hi); }
int b_search(int* arr, int data,int(*compare)(int,int),int start, int end){ if(compare(start,end) == 0) return -1; int mid = (start+end)/2; if(arr[mid] < data){ return b_search(arr,data,compare,mid+1,end); } else if (compare(arr[mid],data) == 0) { return 1; } else { return b_search(arr,data,compare,start,mid); } };
int b_search(int *arr, int data,int(*compare)(int,int), int start, int end){ if(end <start){ return 0; } int middle = (start + end)/2; int comp = compare(data,arr[middle]); if(comp){ return b_search(arr,data,compare,start,middle-1); } else if(comp == -1){ return b_search(arr, data,compare, middle+1, end); } else if(!comp){ return middle; } }
int b_search(vector<double> contain, double value, int start, int end) { int middle = (start+end)/2; if(start > end) { return -1; } if(contain[middle] == value) { return middle; } if(contain[middle] > value) { b_search(contain, value, middle+1, end); } return b_search(contain, value, start, middle-1); }
bool next_permutation(int*s, int sl){ int len = sl, i=len-2; while(i>=0 && s[i]>=s[i+1]) --i; if(i<0) return false; else{ int index=b_search(s, i+1, len-1, s[i]); swap(s, i, index); rev(s, i+1, len-1); return true; } }
int b_search(int *data, int n, int key) { int mid; for (int *p = data, i = 0; i < n; i++, p++) printf("%d ", *p); putchar('\n'); if (n == 1) return (data[0] == key); else { mid = n / 2; printf("compare with %d\n", data[mid - 1]); if (data[mid - 1] == key) return 1; else if (data[mid - 1] > key) return b_search(&data[0], mid, key); else return b_search(&data[mid], n - mid, key); } }
int main() { while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u) > 0) { bool is_solved = true; ans = b_search(lower,upper); if(fabs(upper - ans) < 0.0001 && fabs(func(ans)) > 0.0001) is_solved = false; else if(fabs(lower - ans) < 0.0001 && fabs(func(ans)) > 0.0001) is_solved = false; if(is_solved) printf("%.4lf\n",ans); else printf("No solution\n"); } return 0; }
/* * v_search -- * The search commands. */ static int v_search(SCR *sp, VICMD *vp, CHAR_T *ptrn, size_t plen, u_int flags, dir_t dir) { /* Display messages. */ LF_SET(SEARCH_MSG); /* If it's a motion search, offset past end-of-line is okay. */ if (ISMOTION(vp)) LF_SET(SEARCH_EOL); /* * XXX * Warn if the search wraps. See the comment above, in v_exaddr(). */ if (!KEYS_WAITING(sp)) LF_SET(SEARCH_WMSG); switch (dir) { case BACKWARD: if (b_search(sp, &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags)) return (1); break; case FORWARD: if (f_search(sp, &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags)) return (1); break; case NOTSET: msgq(sp, M_ERR, "189|No previous search pattern"); return (1); default: abort(); } /* Correct motion commands, otherwise, simply move to the location. */ if (ISMOTION(vp)) { if (v_correct(sp, vp, 0)) return(1); } else vp->m_final = vp->m_stop; return (0); }
/* メイン */ int main(void) { int a[N], i, target, work[N], index; for( i = 0; i < N; i++ ) work[i] = rand() % N; #if LINEAR #else quick(work,0,N-1); #endif for( target = N/2-500; target < N/2+500; target++ ) { #if LINEAR index = l_search(work,target); #else index = b_search(work,0,N-1,target); #endif printf("TARGET=%d: ", target); if ( index != -1 ) printf("○\n"); else printf("×\n"); } return 0; }
int main(int argc, char **argv) { char item[RECORD_LEN][ITEM_LEN][STR_LEN]; if (argc < 2){ printf("Please enter the file name!\n"); return 1; } #ifdef DEBUG printf("%d\n", argc); printf("%s\n", argv[0]); printf("%s\n", argv[1]); #endif ReadData(argv[1], item); printf("排序前:\n"); outputData(PRINT_NUM, item); printf("排序后:\n"); sort(item); outputData(PRINT_NUM, item); char str_s[STR_LEN], result[ITEM_LEN][STR_LEN]; printf("\n测试查找功能:\n"); printf("请输入您要查找的字符:"); scanf("%s", str_s); search(item, str_s, result); #ifdef DEBUG printf("你输入的为:%s\n", str_s); #endif printf("\n测试二分查找功能:\n"); printf("请输入您要查找的字符:"); scanf("%s", str_s); b_search(item, str_s, result); return 0; }
void emboss_graph_plot_func(void *obj, seq_reg_plot *plot) { seq_result *result = (seq_result *) obj; out_raster *output = result->output; e_graph *data = result->data; int num_pts = data->n_pts; double sf_m = output->sf_m; double sf_c = output->sf_c; int i, j; double *coords; Tk_Raster *raster; Tcl_CmdInfo info; p_score prev; double x0, y0, x1, y1; int first = -1; int last = -1; double coords_4[4]; #ifdef DEBUG printf("emboss_graph_plot_func\n"); #endif if (output->hidden) { return; } Tcl_GetCommandInfo(output->interp, output->raster_win, &info); raster = (Tk_Raster*)info.clientData; SetDrawEnviron(output->interp, raster, output->env_index); RasterGetWorldScroll(raster, &x0, &y0, &x1, &y1); if (num_pts == 1) { coords = xmalloc(sizeof(*coords) * 2); coords[0] = data->p_array[0].pos; coords[1] = y1 - (sf_m * data->p_array[0].score + sf_c); RasterDrawPoints(raster, coords, 1); } else if (num_pts > 1) { coords = xmalloc(sizeof(*coords) * (2 * (num_pts + 4))); /* * it isn't necessarily the case that I will have as many points as * sequence bases so need to check if each base is within my range */ /* need to find array indices for first and last points */ if (data->p_array[0].pos >= plot->x0) { first = 0; } else { first = b_search(plot->x0, data->p_array, num_pts); if (first > 0) first--; } last = b_search(plot->x1, data->p_array, num_pts); last+=2; if (last > num_pts) last = num_pts; #ifdef DEBUG printf("first %d %d last %d %d\n", first, data->p_array[first].pos, last, data->p_array[last-1].pos); #endif prev = data->p_array[first]; coords[j=0] = prev.pos; coords[j+1] = y1 - (sf_m * prev.score + sf_c) + y0; j += 2; for (i = first+1; i < last; i++, j+=2) { #ifdef DEBUG printf("prev1 %d score %f\n", data->p_array[i].pos, data->p_array[i].score); #endif coords[j] = data->p_array[i].pos; coords[j+1] = y1 - (sf_m * data->p_array[i].score + sf_c) + y0; #ifdef DEBUG printf("coords %f %f\n", coords[j], coords[j+1]); #endif } RasterDrawLines(raster, coords, last-first); xfree(coords); } /* do data and graph objects */ for (i = 0; i < data->n_data_obj; i++) { if (data->d_obj[i].type == E_LINE) { RasterDrawLine(raster, (int)data->d_obj[i].pos.x0, y1 - (sf_m * data->d_obj[i].pos.y0 + sf_c) + y0, (int)data->d_obj[i].pos.x1, y1 - (sf_m * data->d_obj[i].pos.y1 + sf_c) + y0); } else if (data->d_obj[i].type == E_RECTANGLE) { #ifdef DEBUG printf("data rectangle \n"); #endif coords_4[0] = data->d_obj[i].pos.x0; coords_4[1] = y1 - (sf_m * data->d_obj[i].pos.y0 + sf_c) + y0; coords_4[2] = data->d_obj[i].pos.x1; coords_4[3] = y1 - (sf_m * data->d_obj[i].pos.y1 + sf_c) + y0; RasterDrawRectangles(raster, coords_4, 1); } else if (data->d_obj[i].type == E_RECTANGLEFILL) { #ifdef DEBUG printf("data rectangle filled\n"); #endif coords_4[0] = data->d_obj[i].pos.x0; coords_4[1] = y1 - (sf_m * data->d_obj[i].pos.y0 + sf_c) + y0; coords_4[2] = data->d_obj[i].pos.x1; coords_4[3] = y1 - (sf_m * data->d_obj[i].pos.y1 + sf_c) + y0; RasterFillRectangles(raster, coords_4, 1); } else if (data->d_obj[i].type == E_TEXT) { #ifdef DEBUG printf("data text \n"); #endif } } /* * need to do plotting one item at a time because I can't guarentee all * objects will be of the same type */ for (i = 0; i < data->n_graph_obj; i++) { if (data->g_obj[i].type == E_LINE) { RasterDrawLine(raster, (int)data->g_obj[i].pos.x0, y1 - (sf_m * data->g_obj[i].pos.y0 + sf_c) + y0, (int)data->g_obj[i].pos.x1, y1 - (sf_m * data->g_obj[i].pos.y1 + sf_c) + y0); } else if (data->g_obj[i].type == E_RECTANGLE) { #ifdef DEBUG printf("graph rectangle \n"); #endif coords_4[0] = data->g_obj[i].pos.x0; coords_4[1] = y1 - (sf_m * data->g_obj[i].pos.y0 + sf_c) + y0; coords_4[2] = data->g_obj[i].pos.x1; coords_4[3] = y1 - (sf_m * data->g_obj[i].pos.y1 + sf_c) + y0; RasterDrawRectangles(raster, coords_4, 1); } else if (data->g_obj[i].type == E_RECTANGLEFILL) { coords_4[0] = data->g_obj[i].pos.x0; coords_4[1] = y1 - (sf_m * data->g_obj[i].pos.y0 + sf_c) + y0; coords_4[2] = data->g_obj[i].pos.x1; coords_4[3] = y1 - (sf_m * data->g_obj[i].pos.y1 + sf_c) + y0; #ifdef DEBUG printf("graph rectangle filled %f %f %f %f\n", coords_4[j], coords_4[j+1], coords_4[j+2], coords_4[j+3]); #endif RasterFillRectangles(raster, coords_4, 1); } else if (data->g_obj[i].type == E_TEXT) { #ifdef DEBUG printf("graph text \n"); #endif } } }
int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "usage:\t%s <D size > <Q size> <I size> " "<seed>\n", argv[0]); return 1; } int d = atoi(argv[1]); // size of data set D int q = atoi(argv[2]); // size of query set Q int i = atoi(argv[3]); // size of index I int seed = atoi(argv[4]); srand(seed); int *D = (int *) malloc(d * sizeof(int)); int j; for (j = 0; j < d; j++) D[j] = rand(); qsort(D, d, sizeof(int), compare_int); /* Print list for (j = 0; j < d; j++) printf("%d %d\n", j, D[j]); */ int *I = (int *) malloc(i * sizeof(int)); for (j = 1; j < i; j++) { int lo = -1, hi = d; I[ j - 1 ] = D[ ( j*hi + (i - j)*lo ) / i]; } unsigned long bs = 0; unsigned long is = 0; int w = 0; /* Search list */ for (j = 0; j < q; j++) { /* printf("%d\t", r); int a = b_search(r, D, d, -1, d); int b = b_search(r, I, i, -1, i); int lo = -1, hi = d; int new_hi = ( (b+1)*hi + (i - (b+1))*lo ) / i; int new_lo = ( (b)*hi + (i - (b+1))*lo ) / i; if (b == 0) new_lo = -1; else if (b == i) { new_hi = d; new_lo = ( (b-1)*hi + (i - (b+1))*lo ) / i; } int c = b_search(r, D, d, new_lo, new_hi); printf("d:%d\ti:%d\t%d\n",a,b,c); */ printf("b:\n"); int r = rand(); start(); int a = b_search(r, D, d, -1, d); stop(); bs += report(); printf("i:\n"); start(); int b = i_b_search(r, D, d, I, i); stop(); is += report(); if (a != b) ++w; } printf("%lu\t%lu\t%f\n", bs, is, (((double)bs) / ((double)is)) ); return 0; }
int main(void){ int arr[10]={1,2,3,4,5,6,7,8,9,10}; int index=b_search(arr,10,11); printf("%d\n",index ); }
//{{{int i_search(int key, int *I, int I_size, int D_size, int *D_hi, int *D_lo) int i_search(int key, int *I, int I_size, int D_size, int *D_hi, int *D_lo) { int region = b_search(key, I, I_size, -1, I_size); region_to_hi_lo(region, I_size, D_size, D_hi, D_lo); return region; }
void graph_plot_func(void *obj, seq_reg_plot *plot) { seq_result *result = (seq_result *) obj; out_raster *output = result->output; r_graph *data = result->data; int num_pts = data->n_pts; double sf_m = output->sf_m; double sf_c = output->sf_c; int i, j; double coords[2]; Tk_Raster *raster; Tcl_CmdInfo info; p_score prev; double x0, y0, x1, y1; int first = -1; int last = -1; if (output->hidden) { return; } Tcl_GetCommandInfo(output->interp, output->raster_win, &info); raster = (Tk_Raster*)info.clientData; SetDrawEnviron(output->interp, raster, output->env_index); RasterGetWorldScroll(raster, &x0, &y0, &x1, &y1); if (num_pts == 1) { coords[0] = data->p_array[0].pos; coords[1] = y1 - (sf_m * data->p_array[0].score + sf_c); RasterDrawPoints(raster, coords, 1); } else { double *coords = xmalloc(sizeof(*coords) * (2 * (num_pts + 4))); /* * it isn't necessarily the case that I will have as many points as * sequence bases so need to check if each base is within my range */ /* need to find array indices for first and last points */ if (data->p_array[0].pos >= plot->x0) { first = 0; } else { first = b_search(plot->x0, data->p_array, num_pts); if (first > 0) first--; } last = b_search(plot->x1, data->p_array, num_pts); last+=2; if (last > num_pts) last = num_pts; #ifdef DEBUG printf("first %d %d last %d %d\n", first, data->p_array[first].pos, last, data->p_array[last-1].pos); #endif prev = data->p_array[first]; coords[j=0] = prev.pos; coords[j+1] = y1 - (sf_m * prev.score + sf_c) + y0; j += 2; for (i = first+1; i < last; i++, j+=2) { #ifdef DEBUG printf("i %d prev1 %d score %f\n", i, data->p_array[i].pos, data->p_array[i].score); #endif coords[j] = data->p_array[i].pos; coords[j+1] = y1 - (sf_m * data->p_array[i].score + sf_c) + y0; } RasterDrawLines(raster, coords, last-first); xfree(coords); } }