首页 > 代码库 > 二分查找

二分查找

java实现:

package sort;public class BinarySearch {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub                int[] a={70,80,31,37,10,1,48,60,33,80};                    QuickSort.quickSort(a);                System.out.println(binarySearch(a,31));    }        private static int binarySearch(int[] a, int element) {        // TODO Auto-generated method stub                int low,min,upper;                low=0;        upper=a.length-1;                while(low<=upper){            min=(low+upper)/2;                        if(a[min]>element)                upper=min-1;            else if(a[min]<element)                low=min+1;            else                return min;        }                return -1;    }}

 

二分查找