简体   繁体   English

Java 中的 Collections.binarySearch()

[英]Collections.binarySearch() in Java

I'm using the binarySearch() method to find the position of an element in the list.我正在使用 binarySearch() 方法来查找列表中元素的位置。 And I don't understand why the index is -6.我不明白为什么索引是-6。 I see that the element is at the position 1 after sorting in descending order.我看到元素在按降序排序后位于位置 1。 Can somebody tell me why I see the position -6?有人能告诉我为什么我看到位置-6吗? Thank you!谢谢!

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<>();
        al.add(100);
        al.add(30);
        al.add(10);
        al.add(2);
        al.add(50);

        Collections.sort(al, Collections.reverseOrder()); 
        System.out.println(al);

        int index = Collections.binarySearch(al, 50);

        System.out.println("Found at index " + index);
    }
}

The output is: [100, 50, 30, 10, 2]输出为:[100, 50, 30, 10, 2]

Found at index -6在索引 -6 处找到

The list must be ordered into ascending natural order otherwise the results are unpredictable.列表必须按自然升序排序,否则结果不可预测。

From the Javadoc来自Javadoc

Searches the specified list for the specified object using the binary search algorithm.使用二进制搜索算法搜索指定对象的指定列表。 The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(java.util.List) method) prior to making this call.在进行此调用之前,必须根据其元素的自然顺序(如 sort(java.util.List) 方法)将列表按升序排序。 If it is not sorted, the results are undefined .如果未排序,则结果是 undefined If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.如果列表包含多个与指定对象相等的元素,则无法保证会找到哪一个。


Now, if you really want to know why the result is -6, you have to know how the method works internally.现在,如果你真的想知道为什么结果是 -6,你必须知道这个方法在内部是如何工作的。 It takes the mid index and checks wether it's greater or smaller than the value you're searching for.它采用中间索引并检查它是大于还是小于您要搜索的值。

If it's greater (which is the case here), it takes the second half and does the same computation (low becomes middle, and max stays max).如果它更大(这里就是这种情况),它会占用下半部分并进行相同的计算(低变为中,最大值保持最大值)。

At the end, if the key is not found, the method returns -(low + 1) which in your case is -(5 + 1) because the max index becomes the low when there is no way to split it further.最后,如果未找到键,该方法返回-(low + 1) ,在您的情况下为-(5 + 1)因为当无法进一步拆分时,最大索引变为低。

You must either sort in ascending order (per the documentation of Collections.binarySearch), or pass the custom comparator as the third parameter.您必须按升序排序(根据 Collections.binarySearch 的文档),或者将自定义比较器作为第三个参数传递。

int index = Collections.binarySearch(al, 50, Collections.reverseOrder());

Failure to do this will cause undefined results.不这样做将导致未定义的结果。

Apart from the need for the list being sorted, binary Search除了需要对列表进行排序之外,二分查找

returns the index of the search key, if it is contained in the list;返回搜索键的索引,如果它包含在列表中; otherwise, (-(insertion point) - 1)否则, (-(插入点) - 1)

(Javadoc) (Javadoc)

Ie: a negative index designates the negative of the index at which the item being searched would be placed if it were present, minus 1.即:负索引指定索引的负数,如果它存在,则将在该索引处放置正在搜索的项目,减 1。

Ie

-1 means it would be placed at index 0 -1 表示它将被放置在索引 0

-6 means it would be placed at index 5. -6 表示它将被放置在索引 5 处。

Yes, ascending order is important for this algorithm.是的,升序对这个算法很重要。 Just replace sorting with Collections.sort(al) .只需用Collections.sort(al)替换排序即可。

As results you will have:结果,您将拥有:

[2, 10, 30, 50, 100]
Found at index 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM