二分法查找
程序员文章站
2022-03-14 22:55:52
...
import java.util.Arrays;
/**
*
* @项目名称:Test
* @类名称:BinarySearch
* @类描述: 二分法查找
* @创建人:Ansj
* @创建时间:2011-9-13 下午02:53:47
* @修改备注:
* @version
*
*/
public class BinarySearch {
public static void main(String[] args) {
int[] ints = {12,123,21,123,1,432,23,42,3,123,124,3,5435,66,456554,435,423,42,1} ;
int find = 66 ;
//先对数组排序
Arrays.sort(ints) ;
for (int i = 0; i < ints.length; i++) {
new BinarySearch().search(ints, ints[i]) ;
}
}
public void search(int[] ints , int find){
search(ints,0,ints.length,find) ;
}
public void search(int[] ints , int start , int end , int find){
if(ints[(start+end)/2]==find){
System.out.println((start+end)/2);
}else if(ints[(start+end)/2] > find){
search(ints , start , (start+end)/2 , find) ;
}else{
search(ints , (start+end)/2 , end , find) ;
}
}
}
上一篇: 中年星巴克,想用共享空间挽回“打工人”?
下一篇: 二分查找的递归与非递归