【问题标题】:what is the right way of calling a binary search method调用二进制搜索方法的正确方法是什么
【发布时间】:2012-06-02 07:07:30
【问题描述】:

假设我有一个由 10 个整数组成的数组,并且我正在使用二进制搜索来查找一个数字,我们以数字为例

1 2 3 4 5 6 7 8 9 10

我正在使用这种方法

static void binarySearch(int n, int[] a, int low, int high)
    {
        int mid = (high + low) / 2;
        if(low > high)
            System.out.println(n+" was not found after "+counter+" comparisons");
        else if(a[mid] == n)
        {
            counter++;
            System.out.println(n+" was found at position "+mid+" after "+counter+" comparisons");
        }            
        else if(a[mid] < n)
        {
            counter++;
            binarySearch(n, a, mid+1, high);
        }            
        else
        {
            counter++;
            binarySearch(n, a, low, mid-1);
        }            
    }

调用方法 binarySearch(5, a, 0, a.lenght) 的正确方法是什么 要么 binarySearch(5, a, 0, a.lenght-1)

我知道他们都会找到这个数字,但他们会在不同的索引处找到它;从而进行更多比较

【问题讨论】:

  • 你都试过了吗?尝试添加一些调试打印,以便您了解发生了什么。

标签: java search recursion binary


【解决方案1】:

正确的方法是避免使用这种方法,并使用标准的Arrays.binarySearch() 方法,该方法具有记录在案的巨大优势,以及返回结果而不是打印结果的另一个巨大优势在 System.out 上(这使它毫无用处)。

【讨论】:

    【解决方案2】:

    好吧,让我们做一些测试好吗?

    首先,让我们搜索数组中的每个数字。我们得到:

    binarySearch(i, array, 0, array.length);

    1 was found at position 0 after 3 comparisons
    2 was found at position 1 after 4 comparisons
    3 was found at position 2 after 2 comparisons
    4 was found at position 3 after 3 comparisons
    5 was found at position 4 after 4 comparisons
    6 was found at position 5 after 1 comparisons
    7 was found at position 6 after 3 comparisons
    8 was found at position 7 after 4 comparisons
    9 was found at position 8 after 2 comparisons
    10 was found at position 9 after 3 comparisons
    Average: 2.9 comparisons
    

    binarySearch(i, array, 0, array.length - 1);

    1 was found at position 0 after 3 comparisons
    2 was found at position 1 after 2 comparisons
    3 was found at position 2 after 3 comparisons
    4 was found at position 3 after 4 comparisons
    5 was found at position 4 after 1 comparisons
    6 was found at position 5 after 3 comparisons
    7 was found at position 6 after 4 comparisons
    8 was found at position 7 after 2 comparisons
    9 was found at position 8 after 3 comparisons
    10 was found at position 9 after 4 comparisons
    Average: 2.9 comparisons
    

    如您所见,确实会出现差异,但平均值保持不变。 现在让我们测试更大的数字:

    100000 items
    binarySearch(i, array, 0, array.length);
    Average: 15.68946 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 15.68946 comparisons
    
    200000 items
    binarySearch(i, array, 0, array.length);
    Average: 16.689375 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 16.689375 comparisons
    
    500000 items
    binarySearch(i, array, 0, array.length);
    Average: 17.951464 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 17.951464 comparisons
    

    因此,平均而言,这两种方式都不会发生。为方便起见,我建议使用独占上限版本:binarySearch(i, array, 0, array.length);

    【讨论】:

      【解决方案3】:

      问题可以表述为:边界是右包含 [low, high] 还是右排除 [low, high)?右排他形式具有由 Dijkstra“创立”的长期计算机科学传统。在我看来,它也更优雅一点(a.length 而不是 a.length-1)。

      但是在你的函数中它是 [low, high], a.length-1, 正如你所看到的 low > high (not low >= high) 和 (low, mid-1) (not (low, mid)) .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        • 2019-05-28
        相关资源
        最近更新 更多