考虑到迭代binary search代码的实现:
// 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); 
    } 
} GeeksforGeeks网站上写道:
例如,二进制搜索(迭代实现)具有O(Logn)时间复杂度。
我的问题是,除以2与基数2中的对数有什么关系?什么是彼此之间的关系?我会用一个比萨(阵列)的类比来帮助理解我的问题:
1 pizza - divided into 2 parts = 2 pieces of pizza 
2 pieces of pizza - divide each piece in half = 4 pieces of pizza 
4 pieces of pizza - divide each piece in half = 8 pieces of pizza 
8 pieces of pizza - divide each piece in half = 16 pieces of pizza Logₐb = x
b =对数
a =碱基
x =对数结果
aˣ = b
比萨饼的值是1,2,4,8和16,与对数相似,但我仍然不明白两者之间的关系。对数(b)、基(a)和对数(x)的结果与数组(比萨)的2除法之间的关系是什么?x会是我可以分割数组(比萨)的最终数量吗?或者x是我的数组(比萨饼)的除数?
https://stackoverflow.com/questions/65013220
复制相似问题