Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Algorithm 为什么二进制搜索日志的复杂性在Base2中?_Algorithm_Search_Time Complexity_Binary Search_Logarithm - Fatal编程技术网

Algorithm 为什么二进制搜索日志的复杂性在Base2中?

Algorithm 为什么二进制搜索日志的复杂性在Base2中?,algorithm,search,time-complexity,binary-search,logarithm,Algorithm,Search,Time Complexity,Binary Search,Logarithm,考虑到迭代二进制搜索的实现代码: // Java implementation of iterative Binary Search class BinarySearch { // Returns index of x if it is present in arr[], // else return -1 int binarySearch(int arr[], int x) { int l = 0, r = arr.length -

考虑到迭代
二进制搜索的实现
代码:

// Java implementation of iterative Binary Search 
class BinarySearch { 
    // Returns index of x if it is present in arr[], 
    // else return -1 
    int binarySearch(int arr[], int x) 
    { 
        int l = 0, r = arr.length - 1; 
        while (l <= r) { 
            int m = l + (r - l) / 2; 
  
            // Check if x is present at mid 
            if (arr[m] == x) 
                return m; 
  
            // If x greater, ignore left half 
            if (arr[m] < x) 
                l = m + 1; 
  
            // If x is smaller, ignore right half 
            else
                r = m - 1; 
        } 
  
        // if we reach here, then element was 
        // not present 
        return -1; 
    } 
  
    // Driver method to test above 
    public static void main(String args[]) 
    { 
        BinarySearch ob = new BinarySearch(); 
        int arr[] = { 2, 3, 4, 10, 40 }; 
        int n = arr.length; 
        int x = 10; 
        int result = ob.binarySearch(arr, x); 
        if (result == -1) 
            System.out.println("Element not present"); 
        else
            System.out.println("Element found at "
                               + "index " + result); 
    } 
} 
日志ₐb=x

b
=对数
a
=base
x
=对数结果

aˣ=b


比萨饼片的值是1、2、4、8和16,它们类似于对数,但我仍然无法理解它们之间的关系。对数(
b
)、基数(
a
)与数组(pizza)除以2的对数(
x
)结果之间的关系是什么?
x
是否是我可以分割阵列(比萨饼)的最后数量?或者
x
是我的数组(pizza)的分区数吗?

如果你有一个由16片单位大小的pizza,你多久能将它减半(并扔掉其中一片),直到得到一片单位大小的pizza

回答:log2(16)=4次

如果您有一个长度为n的数组,那么在获得长度为1的数组切片之前,您可以将其减半(并扔掉其中一个)的频率是多少

答复:log2(n)

更一般地说,n元搜索算法与对数有什么关系

日志ₐb=x

b
=要搜索的数组的大小
a
=在一次切割后得到的切片数(除一个外,所有切片均被丢弃)

x
=在得到一块大小为1的比萨饼之前,您需要进行的切割次数

让我们使用与您相同的比萨饼类比,假设我们有一整块比萨饼,我们想要8块。每次切割时,我们也要除以2

第一次切割意味着我们将有两个切片。第二次切割为4片。第三次切割产生8个切片。我们切了3块,一共切了8块。从数学上讲,它与数字2、3和8有关系。log函数相应地连接这些数字。当我们被限制在可以划分的范围内时,这就是我们的基数(基数=2)。我们的数量是8。操作数为lg(8)=3(使用lg作为基数2的对数)


同样的想法也适用于二进制搜索。我们将搜索的数组的每个部分除以2,数量与数组的大小无关,我们执行的操作数为渐近lg(n)。

考虑到答案、评论和以下视频:



@mob.评论:

问题不是:要得到16个切片需要多少次切割。而是:要得到一块1号大小的切片需要多少次切割?那是4。换句话说,就像在算法中一样,在每一步中,你把一半切成两半,然后扔掉其中一个。对于大小为16的数组,您可以多久执行一次

@Yves Daoust评论: 一个数字的对数大约是你可以将它减半直到达到1的次数

我的结论是:

大小为n的数组的对数大约是我们可以将它分成两半的次数(考虑基数=2),直到它达到大小为1的最小单位

If (x = Logₐb) then 1*2ˣ = n
So x = # times you can multiply 1 by 2 until you get to n
Reversing Logic: x = # of times you can divide n by 2 until you get to 1


图中的示例是Log₂10=x,其中x是不精确的结果。然而,如果我画了16个位置的数组,这将意味着Log₂16=4,结果4是层数或分割数。

与您的想法相反,
O(log(n))
与任何基数无关。

每次我们切割时,我们也会除以2。在前两次切割后,您必须折叠披萨才能保持真实,这是一个糟糕的例子:(用普通的比萨饼切割,每一次切割增加2片,而不是乘以2。”GAMJAMA,谢谢你的帮助,但是3次切割得到6片而不是8片。考虑<代码> Log2(16)=i < /代码> 2。无论你用什么样的基准做原木,都是一样的。谢谢你的帮助!但是,我的比萨饼要有16块,就需要8块。事实上,我不知道我是否在对数、切数和比萨饼片数之间建立了错误的关系。你一直问错问题。问题不是:切多少块是得到16片所必需的。但是,更确切地说:要得到一片大小为1的比萨饼,需要切割多少次?这就是4。换句话说,就像在算法中一样,你每一步都要将一半的比萨饼切成两半,然后扔掉其中一块。你能用一个大小为16的数组多久做一次?@ricardoramos不是切割多少次,比萨饼已经切割成16片了。Ta让其中一半得到8个。第二次取一半,得到4个。第三次取一半,得到2个。第四次取一半,得到一个切片。这反映了在二进制搜索过程中发生的情况-如果你有一个由16人组成的数组,找到一个特定的一个,将需要(最多)4个操作,每个操作将(上一个)操作的一半搜索空间。@MoB。非常感谢您的帮助!您的评论帮助我更好地理解了。此外,我还使用了以下答案:并且从2:30开始观看了视频。确切地说是@Yves Daoust,但我更怀疑的是对数和二进制搜索算法之间的关系。@ricardoramos:a的对数数字大致是您可以将其减半直至达到1的次数。
If (x = Logₐb) then 1*2ˣ = n
So x = # times you can multiply 1 by 2 until you get to n
Reversing Logic: x = # of times you can divide n by 2 until you get to 1