-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSearch.c
72 lines (51 loc) · 1.45 KB
/
BSearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
int main()
/* 测试主函数 */
{
int list[100],n,i;
int input,low,high,position;
int BSearch(int list[],int input,int low,int high);
printf("输入数字的个数:\n");
scanf("%d",&n);
printf("输入一个数组(有序输入):\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
printf("输入你要查找的数字:\n");
scanf("%d",&input);
low=0;
high=n-1;
position=BSearch(list,input,low,high);
printf("该数字在数组的第%d个\n",position+1);
return 0;
}
/* 利用递归的方法进行二分查找 */
int BSearch(int list[],int input,int low,int high)
{
int mid;
mid=(high+low)/2;
if(low>high)
return -1; //查找失败
else if(input == list[mid])
return mid;
else if(input < list[mid]) //x在左半区查找
return BSearch(list,input,low,mid-1);
else //x在右半区查找
return BSearch(list,input,mid+1,high);
}
/* 利用非递归的方法进行二分查找 */
int BSearch2(int list[],int input,int low,int high)
{
int mid;
while(low <= high)
{
mid=(low+high)/2;
if(input == list[mid])
return mid;
if(input < list[mid])
high=mid-1;
else
low=mid+1;
}
return -1; //查找失败
}