首页 > 其他 > 详细

二分法查找

时间:2014-08-23 17:39:21      阅读:351      评论:0      收藏:0      [点我收藏+]
 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 = {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 }

 

二分法查找

原文:http://www.cnblogs.com/ryanwangblog/p/3931519.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!