java 二分查找算法

来源:互联网 发布:淘宝整点秒杀怎么设置 编辑:程序博客网 时间:2024/05/01 00:24

编程之美在于算法之美,先来看看二分查找的算法:

     隐藏条件:二分查找必须是有序的,从小到大,或从大到小的排序才能进行二分查找,下面来看看代码:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.cn.daming;  
  2.   
  3. public class MainActivity {  
  4.       
  5.   
  6.     /** 
  7.      * @param args 
  8.      */  
  9.     public static void main(String[] args) {  
  10.         int[] aInts = new int[]{1,3,5,8,11,14,16,24,37,47,49,56,63,83,223}; //排序是从小到大  
  11.         int[] aInts2 = new int[]{322,243,211,156,98,85,79,68,53,47,38,24,13,6,2}; //排序是从大到小  
  12.         // TODO Auto-generated method stub  
  13.         MainActivity main = new MainActivity();  
  14.         System.out.println("aInts  the reault is : " + main.binarySearch(aInts, 0, aInts.length, 24));  
  15.         System.out.println("aInts2 the reault is : " + main.binarySearch2(aInts2, 0, aInts2.length, 243));  
  16.     }  
  17.   
  18.     //从小到大的二分查找  
  19.     private int binarySearch(int[] a, int start, int len, int key) {  
  20.         int high = start + len, low = start - 1, guess;  
  21.   
  22.         while (high - low > 1) {  
  23.             guess = (high + low) / 2;  
  24.   
  25.             if (a[guess] < key)  
  26.                 low = guess;  
  27.             else  
  28.                 high = guess;  
  29.         }  
  30.   
  31.         if (high == start + len)  
  32.             return ~(start + len);  
  33.         else if (a[high] == key)  
  34.             return high;  
  35.         else  
  36.             return ~high;  
  37.     }  
  38.       
  39.     //从大到小的二分查找  
  40.     private int binarySearch2(int[] a, int start, int len, int key) {  
  41.         int high = start + len, low = start - 1, guess;  
  42.   
  43.         while (high - low > 1) {  
  44.             guess = (high + low) / 2;  
  45.   
  46.             if (a[guess] > key)  
  47.                 low = guess;  
  48.             else  
  49.                 high = guess;  
  50.         }  
  51.   
  52.         if (high == start + len)  
  53.             return ~(start + len);  
  54.         else if (a[high] == key)  
  55.             return high;  
  56.         else  
  57.             return ~high;  
  58.     }  
  59. }  

返回值就是返回要查的key的下标;

看结果如图:



2014-05-10 11:06 补充:

以上的二分查找理解起来比较费劲,下面我用个可读性强的二分查找,效率估计没有上面的高,但是程序的可读性会很强,

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.       /** 
  2.  * Java Binary search Algorithm 
  3.  *  
  4.  * @param a 
  5.  * @param key 
  6.  * @return Subscript 
  7.  */  
  8. public int binarySearch(int[] a, int key) {  
  9.     if (a.length == 0)  
  10.         return -1;  
  11.     int first = 0;  
  12.     int last = a.length - 1;  
  13.     int mid;  
  14.     while (first <= last) {  
  15.         mid = (first + last) / 2;  
  16.         if (a[mid] == key) {  
  17.             return mid;  
  18.         } else if (a[mid] > key) {  
  19.             last = mid - 1;  
  20.         } else {  
  21.             first = mid + 1;  
  22.         }  
  23.     }  
  24.     return -1;  
  25. }  


这个binarySearch()返回的是数组的下标,我验证过,没有问题的,可以直接copy过来使用的。

       对二分查找求平均查找长度二分查找的过程相当与一棵二叉排序树,所以总节点数为n=2^h-1,h=Log2 (n+1)。 第i层上的节点数为2^(1-1);在等概率的情况下,平均查找长度ASL=Log2 (n+1)-1。

0 0
原创粉丝点击