折半查找

 1 #include<stdio.h>
 2 #define N 15
 3 int Search_array(int array[],int n,int value)
 4 {
 5     int low=0;
 6     int high=n-1;
 7     int mid;
 8     while(low<=high)
 9     {
10         mid=(low+high)/2;
11         printf("左值,右值,中间值[%d,%d,%d]\n",low,high,mid);
12         if(value==array[mid])
13         {
14             return mid;
15         }
16         else if(value<array[mid])
17         {
18             high=mid-1;
19         }
20         else
21         {
22             low=mid+1;
23         }
24     }
25     return -1;
26 }
27 int main()
28 {
29     int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
30     int key;
31     scanf("%d",&key);
32     int i=Search_array(a,N,key);
33     if(i!=-1)
34     {
35         printf("查找成功,%d在数组中的下标为 %d",key,i);
36     }
37     else
38     {
39         printf("查找失败");
40     }
41     return 0;
42 }

 

折半查找

上一篇:【K8s任务】列出集群中所有运行容器的镜像


下一篇:CGTech.Vericut?.v7.3.Win32_64 1DVD