二分搜索算法(JAVA Code)

算法是IT从业人员必修的一门学科,好的算法可以让你的程序提高效率!精通算法,是名企的一个敲门砖!

所以从今天开始,我要将算法复习一遍,并且用JAVA代码来实现!

 

二分搜素算法

采用了分而治之的原理,在使用该算法之前,必须将你的数组进行排序!否则会出现错误的结果!

思想 通过与数组内某个元素(array[i])进行比较,如果比之(array[i])小,则搜索的目标一定在该元素(array[i])之前,否则在(array[i])之后,搜索的范围将缩一半!这样来提搞效率!

 

首先定义一个接口,以后的Search类型都实现这个接口,这样才能不遗漏各种类型的search方法:

 

 public interface SearchUtil { public int search(byte[] array,byte target); public int search(char[] array,char target); public int search(short[] array,short target); public int search(int[] array,int target); public int search(long[] array,long target); public int search(float[] array,float target); public int search(double[] array,double target); public int search(Comparable[] array,Comparable target); }

 

然后用实现类实现以上接口:

 

public class BinarySearch implements SearchUtil{ private static SearchUtil search; public static SearchUtil getInstance(){ if(null==search) return new BinarySearch(); else return search; } //all the functions of basic data type parameters are the same //so i just give a comment in one of them public int search(byte[] array,byte target){ if(array==null||array.length==0) return -1; //the start index,using for reducing the range for searching int start=0; //the end index,using for reducing the range for searching int end=array.length-1; //the index of the element which we will compare to the target int middle=0; //if start<=end means that we have search all the elements while(start<=end){ //every time we use the element which in the middle of the start and end to compare with target middle=(start+end)/2; if(array[middle]<target) //if the element is smaller than the target we will search //the part from middle+1 to end start=middle+1; else if(array[middle]==target) //if the element equals the target, just return the index-middle return middle; else //if the element is larger than the target we will search //the part from start to middle-1 end=middle-1; } //at last because we use this sentence “middle=(start+end)/2” //this always make the element not just in the middle of the start and end,but just one element front //for example: if start=1 end=2 the middle will be 1 so the 2 is never used //so we must fix it if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(char[] array,char target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(short[] array,short target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(int[] array,int target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(long[] array,long target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(float[] array,float target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(double[] array,double target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; while(start<=end){ middle=(start+end)/2; if(array[middle]<target) start=middle+1; else if(array[middle]==target) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==target) return middle; } return -1; } public int search(Comparable[] array,Comparable target){ if(array==null||array.length==0) return -1; int start=0; int end=array.length-1; int middle=0; if(target==null){ //null is smaller the any object which is not null //so when the element is not null,we just search front it while(start<=end){ middle=(start+end)/2; if(array[middle]==null) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==null) return middle; } }else{ int rs=0; while(start<=end){ middle=(start+end)/2; if(array[middle]==null) //null is smaller the any object which is not null //we make the rs<0 ,so we just search behind it rs=-1; else rs=array[middle].compareTo(target); if(rs<0) start=middle+1; else if(rs==0) return middle; else end=middle-1; } if(middle<array.length-1){ middle++; if(array[middle]==null) return -1; else if(array[middle].compareTo(target)==0) return middle; } } return -1; } }

 

点赞