现在的位置: 首页 > 综合 > 正文

折半查找法

2018年09月13日 ⁄ 综合 ⁄ 共 306字 ⁄ 字号 评论关闭
#include <stdio.h>
int halfsearch(int a[],int n,int x);
int main()
{
 int a[10]={4,5,7,8,10,12,16,18,20,30},n,x;
 scanf("%d",&x);
 if(n=halfsearch(a,10,x)!=-1)
  printf("%d %d",x,n);
 else
  ;
 return 0;
 

 

}

int halfsearch(int a[],int n,int x)
{
 int s=0,e=n-1,m;
 while(s<=e)
 {
  m=(s+e)/2;
  if(a[m]==x)
   return m;
  else if(x>a[m])
   s=m+1;
  else
   e=e-1;
 }
 return -1;
}

 

【上篇】
【下篇】

抱歉!评论已关闭.