int bi_search(int numbers[], int b_n, int e_n, int val) { if (b_n > e_n) return 0; int mid = (b_n + e_n) / 2; if (numbers[mid] == val) { return 1; } // 这里二分查找明显不是最优,因为numbers已经排好序,所以下面只需要查找一侧即可 if (bi_search(numbers, b_n, mid-1, val) || bi_search(numbers, mid+1, e_n, val)) { return 1; } return 0; }
int bi_search(vector<int>& v, int i, int j, int target) { int mid = (i+j)/2; if (i>j && i>=v.size()) // right out return 2; else if (i>j && j<0) // left out return -1; else if (i > j) // not found return 0; if (v[mid] == target) return 1; else if (v[mid] < target) return bi_search(v, mid+1, j, target); else if (v[mid] > target) return bi_search(v, i, mid-1, target); // return 1 found, -1 less, 2 biger, 0 not found }
int main() { SeqList sl; int i, j, k, t; printf("input the elements in escendant order:\n"); i = 0; scanf("%d", &(sl.data[i])); while(i < N-1){ scanf("%d", &t); if(t > sl.data[i]){ i++; sl.data[i] = t; }else{ printf("error, must bigger than %d\n", sl.data[i]); } } sl.length = N; printf("input the key:\n"); scanf("%d", &k); j = bi_search(&sl, k); printf("-------------------\n"); if(j >= 0){ printf("found : data[%d]\n", j); }else{ printf("%d is not been found\n", k); } return 0; }
int bi_delete(int key, int a[], int *np) { int p; int i; if (*np > 0) { if ((p = bi_search(key, a, *np)) < 0) return -1; for (i = p+1; i < *np; i++) a[i-1] = a[i]; (*np)--; return p; } return -1; }
void li_test(int a[], int n) { int i; long t1, t2; t1 = get_tick(); for (i = 0; i < LOOP; i++) bi_search(random(30000), a, n); t2 = get_tick(); printf("\n li_search %d loops ellapse %ld ticks.", LOOP, diff_tick(t1,t2)); t1 = get_tick(); for (i = 0; i < LOOP; i++) bii_search(random(30000), a, n); t2 = get_tick(); printf("\n lfi_search %d loops ellapse %ld ticks.", LOOP, diff_tick(t1,t2)); }
bool searchMatrix(vector<vector<int>>& matrix, int target) { if (matrix.empty()) return false; int n = matrix[0].size(); int i = 0; int j = matrix.size() -1; while (i <= j) { int mid = (i+j)/2; int flag = bi_search(matrix[mid], 0, n, target); if (flag == 1) return true; if (flag == 0) return false; if (flag == -1) j = mid-1; if (flag == 2) i = mid+1; } return false; }
void interpret(int a[], int *np, int N) { char command[2]; char key[2]; int p; while (command[0] != 'q') { printf("\nInput command ->"); scanf("%1s", command); scanf("%1s", &key); switch (command[0]) { case 'i' : case 'I' : if (*np < N) { bi_insert(key[0], a, np); printf("\n Successful insert."); } else printf("\n Error : Table full."); break; case 'd' : case 'D' : if (bi_delete(key[0], a, np) < 0) printf("\n Error : Table Empty."); else printf("\n Successful delete."); break; case 's' : case 'S' : if ((p = bi_search(key[0], a, *np)) < 0) printf("\n Error : Can'f find that key"); else printf("\n Ok! find in %d th position", p); break; case 'f' : case 'F' : if ((p = bii_search(key[0], a, *np)) < 0) printf("\n Error : Can't find that key"); else printf("\n Ok ! find in %d th position & move front", p); break; case 'l' : case 'L' : bi_list(a, *np); break; } printf(" n = %d", *np); } }
int *twoSum(int numbers[], int n, int target) { int i = 0, j = n -1, k, sub_val; int ans[2]; int *tmp = (int *)malloc(n * sizeof(int)); memcpy(tmp, numbers, n * sizeof(int)); qsort((void *)numbers, n, sizeof(int), compare); if(false) { //通过二分查找确定 for (i = 0; i < n; ++i) { sub_val = target - numbers[i]; if (bi_search(numbers, i+1, n-1, sub_val)) { break; } } } else { //通过两端遍历查找确定 while(i < j) { if(numbers[i] + numbers[j] == target) { sub_val = numbers[j]; break; } else if(numbers[i] + numbers[j] < target) { i++; } else { j--; } } } for (j = 0; j < n; ++j) { if(tmp[j] == numbers[i] || tmp[j] == sub_val) { ans[0] = j+1; break; } } for (j++; j < n; ++j) { if(tmp[j] == numbers[i] || tmp[j] == sub_val) { ans[1] = j+1; break; } } return ans; }