`
liuzhiqiang19890403
  • 浏览: 57571 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

《java Double Find 算法》

 
阅读更多

二分查找法

package com.jx.p2p.service;

 

 

public class DoubleFindTest {

public static void getDoubleFindMethod(int[] omts,int key){//由小到大的数组

//二分查找法是针对已经排好序的数组进行二分查找元素的方法

//二分查找法是通过中间元素来比较要查找的元素,然后通过中间元素的比较来判断接下来判断的数组部分

int start = 0;

int end = omts.length-1;

String indexValues = "无...";

while(start <= end){//为了将整个数组持续比较下去

int tempValues = (start+end)/2;//求当前比较索引对应的数组的中间元素,小于   则   比较左边的数组的中间元素  ;大于   则   比较右边的数组的中间元素

if(key < omts[tempValues]){

end=--tempValues;

}else if(key > omts[tempValues]){

start=++tempValues;

}else{

indexValues = String.valueOf(tempValues);

break;

}

}

System.out.println("所选元素的索引位置为:"+indexValues);

}

 

public static void main(String[] args) {

int resultvalues = 120;

getDoubleFindMethod(new int[]{0,1,2,3,4,8,9,10,15,16,17,23,32,56,98,100},resultvalues);

}

 

}

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics