Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用二进制搜索插入到有序数组_Java_Algorithm_Binary Search - Fatal编程技术网

Java 使用二进制搜索插入到有序数组

Java 使用二进制搜索插入到有序数组,java,algorithm,binary-search,Java,Algorithm,Binary Search,以下是我到目前为止的情况。问题是如何找到插入点。我在纸上做了一些手工追踪,我观察到的最终是下界和上界相等,当下界和上界相等时,插入点的索引总是高于上界和下界的索引 我知道网上有很多解决方案,但我真的很想自己去理解这一点,因为我只是在自己学习的时候学习并记住一些东西,而不是试图找出别人是如何想出解决方案的 如果有人能指引我,那就太好了。另外,一旦我得到了插入点,我就可以做剩下的事情了,那就是把所有的值移动到一个较低的索引,以使插入值成为一个点 public void insert(long

以下是我到目前为止的情况。问题是如何找到插入点。我在纸上做了一些手工追踪,我观察到的最终是下界和上界相等,当下界和上界相等时,插入点的索引总是高于上界和下界的索引

我知道网上有很多解决方案,但我真的很想自己去理解这一点,因为我只是在自己学习的时候学习并记住一些东西,而不是试图找出别人是如何想出解决方案的

如果有人能指引我,那就太好了。另外,一旦我得到了插入点,我就可以做剩下的事情了,那就是把所有的值移动到一个较低的索引,以使插入值成为一个点

   public void insert(long value) {

            int lowerBound = 0;
            int upperBound = nElems - 1;
            int curIn;

            while (true) {
                curIn = (lowerBound + upperBound) / 2;

                if (a[curIn] < value)
                    lowerBound = curIn + 1;
                else
                    upperBound = curIn - 1;

            }
公共空白插入(长值){
int lowerBound=0;
int上限=nElems-1;
int-curIn;
while(true){
库林=(下界+上界)/2;
if(a[curIn]<值)
lowerBound=curIn+1;
其他的
上限=curIn-1;
}

二进制搜索必须检查循环中的三种情况:

  • 搜索的值小于当前中心元素
  • 搜索的值大于当前中心元素
  • 该值等于当前中心元素(此时搜索完成)
  • 下限
    等于
    上限
    且所述位置不等于搜索的值时,二进制搜索应中止

    无论如何,搜索结束的位置就是要插入值的位置:如果该位置的值等于要插入的值,则无论是在此位置还是之后插入。如果较小,则要在此位置之后插入,如果较大,则要在此位置插入

    我不会给出代码,因为OP显然只是想得到一个提示。

    希望这能有所帮助:

    public void insert(long value) {
        int lowerBound = 0;
        int upperBound = nElems - 1;
        int curIn;
    
        while (lowerBound!=upperBound) { //terminating condition - otherwise you will obtain an infinite loop.
            curIn = (lowerBound + upperBound) / 2;
            //take a choice: put your element immediately before a greater element.
            if (a[curIn] <= value) { //also the same value must be managed as more little than the current.
                lowerBound = curIn + 1;
            } else {
                //You cannot skip curIn lowering down the upper limit, curIn should be the position you are looking for.
                upperBound = curIn; // - 1;
            }
        }
    
        insert_in_list( value, curIn ); //do it as you wish. place the new element in the curIn position
    }
    
    公共空白插入(长值){
    int lowerBound=0;
    int上限=nElems-1;
    int-curIn;
    while(lowerBound!=上限){//终止条件-否则将获得无限循环。
    库林=(下界+上界)/2;
    //做出选择:把你的元素放在更大元素的前面。
    
    如果(a[curIn]
    while(true)
    没有
    break
    ?是的,我想在我找到插入点后放break.
    最终下限和上限相等
    为什么不添加一个检查,看看两者是否相等,然后,在循环中的其他检查之前?如果你知道你的基本情况,请检查它。:)是的,这是有道理的,所以如果它们相等,我可以安全地假设插入点比它们相等的索引高1个索引吗?是的,但是你如何确定插入点?因为最终你的下限和上限范围变得足够小,在你降到一个索引的地方…是的,这是最坏情况下发生的。在那个点上t该位置的值可能与您要插入的值相同,也可能不同:如果它较小,您要在该位置之后插入,如果它较大,您要在之前插入。因此,一旦上下限相等,我可以安全地假设插入点高于或低于该值吗?是的,它有这样做是因为我们最初定义数组是排序的。当然,当[curIn]等于您要插入的值时,您也可以假设这一点。然后,curIn就是您要放置该值的位置。这是低效的。当[curIn]等于您要插入的值时,您可能会提前停止。一般来说,这是正确的,但我做了以下假设:“将您的元素立即放在更大的元素之前”。此假设根据插入时间使用相同的值排列元素。我不知道该函数将用于哪个应用程序。我不知道时间是否重要。为了简化并能够提供有用的答案,我做了假设。