码迷,mamicode.com
首页 > 编程语言 > 详细

把元素插入有序数组中

时间:2020-03-09 01:47:41      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:div   折半查找   system   class   ret   插入   public   out   max   

折半查找的应用

public static void main(String[] args) {
        int[] arr = {4,6,6,7,8,9,10};
        System.out.println(getIndexOf(arr,5));
    }

    public static int getIndexOf(int[] arr, int key){
        int min = 0,
                max = arr.length-1,
                mid;
        while (min<=max){
            mid = (max+min)>>1;
            if ( key > arr[mid])
                min = mid+1;
            else if (key < arr[mid])
                max = mid-1;
            else
                return mid;
        }
        return min;
    }

 

把元素插入有序数组中

标签:div   折半查找   system   class   ret   插入   public   out   max   

原文地址:https://www.cnblogs.com/hongxiao2020/p/12445991.html

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