码迷,mamicode.com
首页 > 其他好文 > 详细

二分法查找

时间:2014-08-23 17:39:21      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   art   div   log   

 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 }

 

二分法查找

标签:style   blog   color   os   io   ar   art   div   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!