标签:
package BinarySearch; public class BinarySearch { /** * @param args */ public static void main(String[] args) { int[] sort={1,3,4,5,6,8,9}; int index=binarySearch(sort,9); System.out.println(index); } private static int binarySearch(int[] sort, int data) { if(data<sort[0] || data>sort[sort.length-1]){ return -1; } int begin=0; int end=sort.length; int mid=(begin+end)/2; while(begin<=end){ mid=(begin+end)/2; if(data>sort[mid] ){ begin=mid++; }else if(data<sort[mid]){ end=mid--; }else{ return mid; } } return -1; } }
标签:
原文地址:http://www.cnblogs.com/yaobolove/p/5599300.html