binary search

  1 #include <stdio.h>
  2 
  3 int bi_search(int x,int a[],int n)
  4 {
  5         int low,upper,mid;
  6 
  7         low = 0;
  8         upper = n-1;
  9         while(low <= upper)
 10         {
 11                 mid = (low + upper)/2;
 12                 if(x < a[mid])
 13                         upper = mid-1;
 14                 else if(x > a[mid])
 15                         low = mid+1;
 16                 else return mid;
 17         }
 18         return -1;
 19 }
 20 
 21 void main()
 22 {
 23         int a[5] = {3,4,5,6,7};
 24         printf("%d\n",bi_search(4,a,5));
 25 }

posted on 2012-05-20 22:07 三少_爷 阅读(108) 评论(0)  编辑 收藏 引用 所属分类: the c programming language


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿

随笔分类

随笔档案

My Website

搜索

最新评论

阅读排行榜

评论排行榜