首页 > 代码库 > 二分法查找
二分法查找
1 package com.learning.algorithm; 2 3 public class BinarySearch { 4 5 public int binSearch(int[] arrValue, int start, int end, int key){ 6 int result = -1; 7 8 int mid = (start+end)/2; 9 10 if(start > end){11 return result;12 }13 14 if(arrValue[mid]==key){15 result = mid;16 }else if(key<arrValue[mid]){17 result = binSearch(arrValue,start,mid-1,key);18 }else if(key>arrValue[mid]){19 result = binSearch(arrValue,mid+1,end,key);20 }21 22 return result;23 }24 25 public static void main(String[] args) {26 int[] arrValue = http://www.mamicode.com/{3,5,11,17,21,23,28,30,32,50,64,78,81,95,101}; 27 BinarySearch bs = new BinarySearch();28 int result = bs.binSearch(arrValue,0, arrValue.length-1, 17);29 System.out.println("the position of the array is :"+result);30 }31 }
二分法查找
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。