java二分查找
Posted oldfour
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java二分查找相关的知识,希望对你有一定的参考价值。
java二分查找
import java.util.Arrays; public class TestBianarySearch3 { public static void main(String[] args) { int [] arr = {23,34,45,65,26,76,86,45,765,43}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); System.out.println(BianarySearchResult(arr,45)+1); } public static int BianarySearchResult(int[] arr,int result) { int low = 0; int high = arr.length-1; while(low<=high) { int mid = (low+high)/2; if( arr[mid] == result) { return mid; } if(result<arr[mid]) { high = mid-1; } if(result>arr[mid]) { low = high+1; } } return -1; } }
以上是关于java二分查找的主要内容,如果未能解决你的问题,请参考以下文章