本程序纯粹为测试二分查找,所以以有序数组存储数据来简化程序
- #include <stdio.h>
- void
- b_search(int *a, int j, int low, int high)
- {
- if(low == high){
- if(a[low] == j)
- printf("Find!\n");
- else
- printf("Not find!\n");
- }
- else{
- if(a[(low + high) / 2] < j){
- b_search(a, j, (low + high) / 2 + 1, high);
- }
- else if(a[(low + high) / 2] > j){
- b_search(a, j, low, (low + high) / 2 - 1);
- }
- else
- printf("Find!\n");
- }
- }
- int
- main(int argc, char **argv)
- {
- int i = 0, j;
- int a[10];
- for(;i < 10; i++)
- a[i] = i + 1;
- scanf("%d",&j);
- b_search(a, j, 0, 9);
- return 0;
- }