forked from Vencenter/C_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
查找_折半查找_.cpp
64 lines (52 loc) · 875 Bytes
/
查找_折半查找_.cpp
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
#include <stdio.h>
#include <stdlib.h>
int insertSort(int a[],int low,int high,int key)
{
int count=0;
int mid;
while(low<=high)
{
count++;
mid=(low+high)/2;
//printf("-->%d-->%d-->%d\n",low,high,mid);
if(key==a[mid])
{
printf("查找->%d,在%d个位置,次数-->%d \n",key,mid+1,count);
return mid;
}
else if(key>a[mid])
{
low=mid+1;
}
else if(key<a[mid])
{
high=mid-1;
}
if(low>high)
{
printf("failed\n");
}
//return -1;
}
}
int main()
{
int a[10],i,m;
int num=10;
printf("10 num is:\n");
for(i=0;i<num;i++)
{
a[i]=i+1;
}
for(i=0;i<num;i++)
{
printf("%d ",a[i]);
}
printf("\n");
//printf("please input 查找的数字:\n");
//scanf("%d",&m);
for(i=0;i<num;i++)
{
insertSort(a,0,num-1,i+1);
}
}